BitSet static BitSet valueOf(byte[] bytes) Method Example Program


Returns a new bit set containing all the bits in the given byte array.

Program

package com.candidjava;

import java.util.BitSet;

/**
 * @author : vinod kumar v
 * @description :Returns a new bit set containing all the bits in the given byte
 *              array.
 * **/
public class BitSetStaticBitSetByteBytes {
	public static void main(String[] args) {

		BitSet bitset1 = BitSet.valueOf(new byte[] { 1, 2, 3 });

		System.out.println("Bitset1:" + bitset1);

	}
}

Output

Bitset1:{0, 9, 16, 17}

Explanation

public static BitSet valueOf(byte[] bytes)

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.


This method is equivalent to BitSet.valueOf(ByteBuffer.wrap(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



Related Post

Comments


©candidjava.com