Bit shifting in Ruby

瘦欲@ 提交于 2019-12-22 05:27:11

问题


I'm currently converting a Visual Basic application to Ruby because we're moving it to the web. However when converting some algorithms I've run into a problem concerning bit shifting.

How I understand it, the problem lies in the size mask VB enforces on Integer types (as explained Here). Ruby, in practice, doesn't differentiate in these types.

So the problem:

Visual Basic

Dim i As Integer = 182
WriteLine(i << 24) '-1241513984

Ruby

puts 182 << 24 # 3053453312

I've been Googling and reading up on bit shifting the last hours but haven't found a way, or direction even, to tackle this problem.


回答1:


You need to replicate what visual basic is doing, namely

  • mask the shift value as documented
  • cap mask the result with 0xFFFFFFFF (since ruby will have promoted the value to a bignum for you
  • if the top most bit is set, subtract 2^32 from the result (since signed integers are stored with 2s complement

For example

def shift_32 x, shift_amount
  shift_amount &= 0x1F
  x <<= shift_amount
  x &= 0xFFFFFFFF 

  if (x & (1<<31)).zero?
   x
  else
   x - 2**32
  end
end


来源:https://stackoverflow.com/questions/9445760/bit-shifting-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!