Ruby unsigned right shift operator
I'm attempting to convert so开发者_如何学JAVAme of my Java code to (J)Ruby, and due to my lack of experience of bitwise operations, I ran into a problem that I can't seem to be able to solve by myself.
Simply put, I don't know how to convert this piece of Java code into Ruby, as Ruby does not appear to have the unsigned right shift operator (>>>).
private static short flipEndian(short signedShort) {
int input = signedShort & 0xFFFF;
return (short) (input << 8 | (input & 0xFF00) >>> 8);
}
def self.flip_endian(signed_short)
input = signed_short & 0xFFFF
input << 8 | (input & 0xFF00) >> 8
end
This will swap the first 2 bytes and cut off all the higher bits of an Integer:
def self.flip_endian(input)
input << 8 & 0xFF00 | input >> 8 & 0xFF
end
精彩评论