Need to reverse all the bits in a byte []. I'm doing this in two steps.
- Reverse all the bytes in this array.
- Reverse all the bits in each byte.
Is this the most efficient way to do this?
public byte[] reverse(byte [] data) {
byte [] bytes = data.clone();
for (int i = 0; i < bytes.length / 2; i++) {
byte temp = bytes[i];
bytes[i] = bytes[bytes.length - i - 1];
bytes[bytes.length - i - 1] = temp;
}
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (Integer.reverse(bytes[i]) >>> 24)
}
return bytes;
}
- The flipping is used as a primitive hashing function.
- I realized that I was flipping the bits not reversing them, this has been fixed.
- The bitwise '&' was unnecessary and has been removed.