BigInt icon indicating copy to clipboard operation
BigInt copied to clipboard

Signed bitwise shift

Open esen opened this issue 4 years ago • 1 comments

Signed bitwise shift doesn't work.

Example:

Int(65764) >> 16   // gives 1
Int(-65764) >> 16   // gives -2

BigInt(65764) >> 16   // gives -1
BigInt(-65764) >> 16   // gives -1, must be -2

esen avatar May 07 '21 08:05 esen

Thanks for brining this up. Our Signed Big Integer implementation uses a sign bit and an unsigned magnitude. When we perform a right shift on a BigInt we basically just shift the magnitude right.

In contrast, right shifting Int is a right shift on the Two's complement representation of that integer, where the top most bits are filled with ones.

The behavior of a right shift of negative integers is in theory implementation dependent. Though I think it could be beneficial for the BigInt implementation to behave like the Swift Int implementation (and the majority of other implementations e.g. python, ...).

tgymnich avatar May 11 '21 19:05 tgymnich