开发者

Ruby unsigned right shift operator

开发者 https://www.devze.com 2023-04-04 06:34 出处:网络
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 myse

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号