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 array.

More precisely,

BitSet.valueOf(bytes).get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)
for all n < 8 * bytes.length

Syntax

public static BitSet valueOf(byte[] bytes)

Parameters

bytes Specify a byte array containing sequence of bits to be used as the initial bits of the new bit set.

Return Value

Returns a BitSet containing all the bits in the byte array.

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 array.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a byte array
    byte Arr[] = {10, 5, 25};

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

    //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