Returns a new byte array containing all the bits in this bit set.
Program
package com.candidjava;
import java.util.Arrays;
import java.util.BitSet;
/**
* @author : vinod kumar v
* @description :Returns a new byte array containing all the bits in this bit
* set.
* **/
public class bitSetByteToByteArray {
public static void main(String[] args) {
BitSet bitset1 = new BitSet(8);
bitset1.set(0);
bitset1.set(1);
bitset1.set(2);
System.out.println("Bitset1:" + bitset1);
System.out.println(Arrays.toString(bitset1.toByteArray()));
}
}
Output
Bitset1:{0, 1, 2}
[7]
Explanation
public byte[] toByteArray()
Returns a new byte array containing all the bits in this bit set.
More precisely, if
byte[] bytes = s.toByteArray();
then bytes.length == (s.length()+7)/8 and
s.get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)
for all n < 8 * bytes.length.
Returns:
a byte array containing a little-endian representation of all the bits in this bit set