Bit shift operations work incorrectly
slashOwO opened this issue ยท 2 comments
bitwise_shift_left(num, amount)
and bitwise_shift_right(num, amount)
returns num << (amount & 63)
or num >> (amount & 63)
when amount
is negative or greater than 63.
bitwise_shift_left(1, 64) => 1, should be 0
bitwise_shift_right(1, 64) => 1, should be 0
bitwise_shift_left(1, -1) => -9223372036854775808, should be 0
bitwise_shift_right(1, -1) => 0, shuould be 2
maybe bitwise_shift_right
should be implement with >>>
instead of >>
.
bitwise_roll_left
and bitwise_roll_right
can be implement with Long.rotateLeft
and Long.rotateRight
.
They're implemented by the Java >>
operators, so in fact they only care about the LSBs of the shift amount. I think it makes sense for the right one to use logic shift, though I think an arithmetic shift would then be needed.
I agree about the roll implementation using the rotate methods. Thanks for checking!
Splitting the mask to the amount to #1675.