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

math/big: Mention sign inversion in the (*Int).Not documentation #29949

Open
ainar-g opened this issue Jan 26, 2019 · 3 comments
Open

math/big: Mention sign inversion in the (*Int).Not documentation #29949

ainar-g opened this issue Jan 26, 2019 · 3 comments

Comments

@ainar-g
Copy link
Contributor

@ainar-g ainar-g commented Jan 26, 2019

I would expect that this:

i = big.NewInt(-2)
log.Println(new(big.Int).Not(i).Text(2))

prints -1, but actually it prints 1. The (*Int).Not documentation simply says:

Not sets z = ^x and returns z.

I think that it's not obvious from this sentence that the sign is inverted.

@ericlagergren

This comment has been minimized.

Copy link
Contributor

@ericlagergren ericlagergren commented Jan 28, 2019

This matches how Go handles signed integer negation: https://play.golang.org/p/BhiwKrHhVj2

^x bitwise complement is m ^ x with m = "all bits set to 1" for unsigned x
and m = -1 for signed x

If it’s a problem with Not, perhaps the doc fix should apply equally to all bitwise methods.

@ainar-g

This comment has been minimized.

Copy link
Contributor Author

@ainar-g ainar-g commented Jan 28, 2019

I know that the &^ does that, but it does that because of how signed integers work. big.Int doesn't have a “highest bit”, so the behaviour is rather surprising, at least to me.

@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Mar 1, 2019

CC @griesemer for math/big

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

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.