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
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@ainar-g
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor Author

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
Copy link
Contributor

bcmills commented Mar 1, 2019

CC @griesemer for math/big

@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 1, 2019
@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants