1

If I create a random byte[], for instance:

byte[] b = new byte[16];
new Random().nextBytes(b);

That means that I have 16 bytes or 128 bits of data, right?

Is there a way to read the bit in position X so that I can learn if it's 0 or 1?

This question is similar, but not the same as, an existing question that asks how to get a bit in a byte. But I want a bit in a byte array, byte[].

Community
  • 1
  • 1
Aventinus
  • 1,322
  • 2
  • 15
  • 33

2 Answers2

4

That means that I have 16 bytes or 128 bits of data, right?

Right!

You can get a bit in your byte array like this :

int readBit(byte[] b, int x) {
   int i = x / 8;
   int j = x % 8;
   return (b[i] >> j) & 1;
}
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
0

In java you can not aaccess bits directly. You need to use bitwise operators.

So you could compare the byte whit a nother byte whose bit value you alleready know.

mat
  • 77
  • 1
  • 1
  • 6