Convert a bitset to an int. - Java java.util

Java examples for java.util:BitSet

Description

Convert a bitset to an int.

Demo Code


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

public class Main {
    /**/*from  www. j  a v  a  2s.c om*/
     * Convert a bitset to an int.
     * 
     * @param bs The bitset.
     * 
     * @return the corresponding int.
     */
    public static int ToInt(BitSet bs) {

        int pow = 1;
        int value = 0;
        for (int i = 0; i < bs.length(); i += 1) {
            if (bs.get(i))
                value += pow;
            pow *= 2;
        }
        return value;
    }
}

Related Tutorials