Convert a BitSet to an unsigned integer. - Java java.util

Java examples for java.util:BitSet

Description

Convert a BitSet to an unsigned integer.

Demo Code


//package com.java2s;

import java.util.BitSet;

public class Main {
    /**//from w w  w .  j  av a  2  s .c o  m
     * Convert a BitSet to an unsigned integer.
     *
     * @param b BitSet to convert.
     * @param startBit LSB bit of the integer.
     * @param length Number of bits to read.
     * @return an unsigned integer number.
     */
    public static int bitSetToUnsignedInt(BitSet b, int startBit, int length) {
        int val = 0;
        int bitval = 1;
        for (int i = 0; i < length; i++) {
            if (b.get(startBit + i))
                val += bitval;
            bitval += bitval;
        }

        return val;
    }
}

Related Tutorials