Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signed bitwise shift #81

Open
esen opened this issue May 7, 2021 · 1 comment
Open

Signed bitwise shift #81

esen opened this issue May 7, 2021 · 1 comment

Comments

@esen
Copy link

esen commented May 7, 2021

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
@tgymnich
Copy link
Collaborator

tgymnich commented May 11, 2021

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, ...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants