What does this piece of code mean?
I have to port a piece of Java code to .NET. I don't fully understand the following piece:
public void setBytes(byte bytes[]) {
this.bytes = bytes;
int len = bytes.length * 8;
bits = new byte[len];
for(int i = 0; i < bytes.length; i++) {
for(int k = 7; k >= 0; k--) {
bits[--len] = (byte) ((bytes >>> k) & 0x01);
}
}
}
Can anyone explain the last line to me:
bits[--len] = (byte) ((bytes >>> k) & 0x01).
What is '>>>'?
Greets,
Nils Gruson
[594 byte] By [
ngrusona] at [2007-10-2 9:06:41]

The line
bits[--len] = (byte) ((bytes >>> k) & 0x01);
is extracting the individual bits of each byte in reverse order. For example if
byte[] bytes = {'a','b','c','d','e'};
bits would contain: 1010011000100110110001100100011010000110
Note that the first 8 bits (10100110) correspond to the 'e' which is 95 (01100101) in ASCII. The next 8 bits correspond to the 'd', etc...
The >>> is a right shift with 0 fill; the >> would fill with the sign bit. Since you are "anding" with 0x01, you are only picking up the rightmost bit so >> would work here as well.