Java Utility Library

Java BitSet - valueOf() Method



The java.util.BitSet.valueOf() method returns a new bit set containing all the bits in the given byte buffer between its position and limit.

More precisely,

BitSet.valueOf(bb).get(n) == ((bb.get(bb.position()+n/8) & (1<<(n%8))) != 0)
for all n < 8 * bb.remaining()

Syntax

public static BitSet valueOf(ByteBuffer bb)

Parameters

bb Specify a byte buffer containing a sequence of bits between its position and limit, to be used as the initial bits of the new bit set.

Return Value

Returns a BitSet containing all the bits in the buffer in the specified range.

Exception

NA.

Example:

In the example below, the java.util.BitSet.valueOf() method returns a BitSet which contains all the bits in the specified byte buffer.

import java.util.*;
import java.nio.ByteBuffer;  

public class MyClass {
  public static void main(String[] args) {
    //creating a byte buffer
    ByteBuffer bb = ByteBuffer.wrap(new byte[]{10, 5, 25});

    //creating bit set from given byte buffer
    BitSet BSet = new BitSet();
    BSet = BitSet.valueOf(bb);

    //printing BitSet
    System.out.println("BSet contains: " + BSet);
  }
}

The output of the above code will be:

BSet contains: {1, 3, 8, 10, 16, 19, 20}

❮ Java.util - BitSet