Java BitSet valueOf(byte[] bytes) Method



Description

The Java BitSet valueOf(byte[] bytes) method return a new bit set containing all the bits in the given byte array. This method is same as BitSet.valueOf(ByteBuffer.wrap(bytes)).

Declaration

Following is the declaration for java.util.BitSet.valueOf(byte[] bytes) method

public static BitSet valueOf​(byte[] bytes)

Parameters

bytes− a byte array containing a little-endian representation of a sequence of bits to be used as the initial bits of the new bit set.

Return Value

This method returns a BitSet containing all the bits in the byte array.

Exception

NA

Java BitSet valueOf(long[]) Method

Description

The Java BitSet valueOf(long[] longs) method return a new bit set containing all the bits in the given long array. This method is same as BitSet.valueOf(LongBuffer.wrap(longs)).

Declaration

Following is the declaration for java.util.BitSet.valueOf(long[] longs) method

public static BitSet valueOf​(long[] longs)

Parameters

longs− a long array containing a little-endian representation of a sequence of bits to be used as the initial bits of the new bit set.

Return Value

This method returns a BitSet containing all the bits in the long array.

Exception

NA

Java BitSet valueOf(ByteBuffer) Method

Description

The Java BitSet valueOf(ByteBuffer bb) method return a new bit set containing all the bits in the given bytebuffer. The byte buffer is not modified with the use of this method, and no reference to the buffer is retained by the bit set.

Declaration

Following is the declaration for java.util.BitSet.valueOf(ByteBuffer bb) method

public static BitSet valueOf​(ByteBuffer bb)

Parameters

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

Return Value

This method returns a BitSet containing all the bits in the buffer in the specified range.

Exception

NA

Java BitSet valueOf(LongBuffer) Method

Description

The Java BitSet valueOf(LongBuffer lb) method return a new bit set containing all the bits in the given long buffer. The long buffer is not modified with the use of this method, and no reference to the buffer is retained by the bit set.

Declaration

Following is the declaration for java.util.BitSet.valueOf(LongBuffer lb) method

public static BitSet valueOf​(LongBuffer lb)

Parameters

lb− a byte buffer containing a little-endian representation of a sequence of bits between its position and limit, to be used as the initial bits of the new bit set.

Return Value

This method returns a BitSet containing all the bits in the buffer in the specified range.

Exception

NA

Example 1

The following example shows the usage of Java BitSet valueOf(byte[]) method. We're creating two BitSets using byte[] and printing them.

package com.tutorialspoint;
import java.util.BitSet;
public class BitSetDemo {
   public static void main(String[] args) {

      // create 2 bitsets
      BitSet bitset1 = BitSet.valueOf(new byte[] { 0, 1, 2, 3, 4, 5 });
      BitSet bitset2 = BitSet.valueOf(new byte[] { 2, 4, 6, 8, 10 });

      // print the sets
      System.out.println("Bitset1:" + bitset1);
      System.out.println("Bitset2:" + bitset2);
   }
}

Let us compile and run the above program, this will produce the following result −

Bitset1:{8, 17, 24, 25, 34, 40, 42}
Bitset2:{1, 10, 17, 18, 27, 33, 35}

Example 2

The following example shows the usage of Java BitSet valueOf(long[]) method. We're creating two BitSets using long[] and then printing the bitsets.

package com.tutorialspoint;
import java.util.BitSet;
public class BitSetDemo {
   public static void main(String[] args) {

      // create 2 bitsets
      BitSet bitset1 = BitSet.valueOf(new long[] { 0, 1, 2, 3, 4, 5 });
      BitSet bitset2 = BitSet.valueOf(new long[] { 2, 4, 6, 8, 10 });

      // print the sets
      System.out.println("Bitset1:" + bitset1);
      System.out.println("Bitset2:" + bitset2);
   }
}

Let us compile and run the above program, this will produce the following result −

Bitset1:{64, 129, 192, 193, 258, 320, 322}
Bitset2:{1, 66, 129, 130, 195, 257, 259}

Example 3

The following example shows the usage of Java BitSet valueOf(ByteBuffer) method. We're creating two BitSets using ByteBuffer objects and printing them.

package com.tutorialspoint;
import java.nio.ByteBuffer;
import java.util.BitSet;
public class BitSetDemo {
   public static void main(String[] args) {

      // create 2 bitsets
      BitSet bitset1 = BitSet.valueOf(ByteBuffer.wrap(new byte[] { 0, 1, 2, 3, 4, 5 }));
      BitSet bitset2 = BitSet.valueOf(ByteBuffer.wrap(new byte[] { 2, 4, 6, 8, 10 }));

      // print the sets
      System.out.println("Bitset1:" + bitset1);
      System.out.println("Bitset2:" + bitset2);
   }
}

Let us compile and run the above program, this will produce the following result −

Bitset1:{8, 17, 24, 25, 34, 40, 42}
Bitset2:{1, 10, 17, 18, 27, 33, 35}

Example 4

The following example shows the usage of Java BitSet valueOf(LongBuffer) method. We're creating two BitSets using LongBuffer objects and printing them.

package com.tutorialspoint;
import java.nio.LongBuffer;
import java.util.BitSet;
public class BitSetDemo {
   public static void main(String[] args) {

      // create 2 bitsets
      BitSet bitset1 = BitSet.valueOf(LongBuffer.wrap(new long[] { 0, 1, 2, 3, 4, 5 }));
      BitSet bitset2 = BitSet.valueOf(LongBuffer.wrap(new long[] { 2, 4, 6, 8, 10 }));

      // print the sets
      System.out.println("Bitset1:" + bitset1);
      System.out.println("Bitset2:" + bitset2);
   }
}

Let us compile and run the above program, this will produce the following result −

Bitset1:{8, 17, 24, 25, 34, 40, 42}
Bitset2:{1, 10, 17, 18, 27, 33, 35}
java_util_bitset.htm
Advertisements