Logical shift¶
Rationale¶
Java and JavaScript have a >>> operator called "logical right shift", which behaves different from the normal >> arithmetical right shift.
To achieve full ECMA compatibility, we need an implementation of logical right shift for AmberVM.
Read about Arithmetic shift and Logical shift on Wikipedia.
In Ruby¶
Ruby doesn't support logical shifting, since it abstracts integer numbers on a virtually infinite bit size; there is no MSB for Ruby Integers.
Here's a sample implementation for Ruby, using the Integer#[] bit query method:
1 class Integer
2 def logical_shift_right positions = 1, bit_size = 32
3 raise ArgumentError, 'negative argument for logical_shift_right' if positions < 0
4 positions = bit_size if positions > bit_size
5 bits = (0...bit_size).map { |i| self[i] }.reverse
6 bits = [0] * positions + bits[0, bit_size - positions]
7 bits.join.to_i(2)
8 end
9 alias lsr logical_shift_right
10 end
11 class Float
12 def logical_shift_right positions = 1, bit_size = 32
13 (to_i % 2 ** bit_size).logical_shift_right positions, bit_size
14 end
15 alias lsr logical_shift_right
16 end
JavaScript seems to act on a bit size of 32, so I took this as default. You can also simulate 64-bit or other machines.
The Float extensions are also for JavaScript, which uses only floating point numbers.