Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bit shifting in Ruby

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.

like image 213
Novae Avatar asked Feb 25 '12 16:02

Novae


1 Answers

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
like image 111
Frederick Cheung Avatar answered Oct 23 '22 09:10

Frederick Cheung