Reputation: 1171
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:
Dim i As Integer = 182
WriteLine(i << 24) '-1241513984
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.
Upvotes: 6
Views: 2416
Reputation: 84132
You need to replicate what visual basic is doing, namely
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
Upvotes: 8