I am trying to create a BitSet
of 64 bits where the first 20 bits are generated randomly skipping the LSB - The LSB has to either be set to 1 or 0 depending on if the number of 1s are odd or even (calculating the parity bits).
Right now, I am able to create 64 bits where the LSB is set to 1 as the number of 1s (0) are even. What I want to achieve is to randomly set the first 20 bits (the MSB I guess).
BitSet temp = new BitSet();
for (int i = 0; i < temp.size() / 8; i++) {
int msb = i * 8;
BitSet group = temp.get(msb, msb + 8);
temp.set(msb + 7, group.cardinality() % 2 == 0);
}
StringBuilder sb = new StringBuilder();
for( int i = 0; i < temp.length(); i++ )
{
sb.append( temp.get( i ) == true ? 1: 0 );
}
System.out.println("Bitset " + sb);
This gives the output:
Bitset 0000000100000001000000010000000100000001000000010000000100000001
Where I want it to be something like:
xxxxxxxp xxxxxxxp xxxxxx0p 0000000p 0000000p 0000000p 0000000p 0000000p
Where x
is the randomly set bit (either 0 or 1) and p
is the parity bit.