Convert a bitset to an integer value - Java java.util

Java examples for java.util:BitSet

Description

Convert a bitset to an integer value

Demo Code


//package com.java2s;
import java.util.BitSet;

public class Main {
    /**//from w  w  w  .java 2  s.c  o m
     * Convert a bitset to an integer value
     * @param bitSetValue The bitset to convert
     * @return the int value of a bit set
     */
    public static int bitSetToInt(BitSet bitSetValue) {
        int returnInt = 0;

        // Loop through int size shifting bytes as needed
        for (int i = Integer.SIZE; i >= 0; i--) {
            if (bitSetValue.get(i)) {
                returnInt |= (1 << i);
            }
        }
        return returnInt;
    }
}

Related Tutorials