BitSet static BitSet valueOf(ByteBuffer bb) Method Example Program


Returns a new bit set containing all the bits in the given byte buffer between its position and limit.

Program

package com.candidjava;

import java.nio.ByteBuffer;
import java.util.BitSet;

/**
 * @author : vinod kumar v
 * @description :Returns a new bit set containing all the bits in the given byte
 *              buffer between its position and limit.
 **/
public class BitSetStaticBitSetValueOfByteBuffer {
	public static void main(String[] args) {

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

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

	}

}

Output

Bitset1:{0, 9, 16, 17}

Explanation

public static BitSet valueOf(ByteBuffer bb)
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().

The byte buffer is not modified by this method, and no reference to the buffer is retained by the bit set.

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


Related Post

Comments


©candidjava.com