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

^0 is interpreted as -1 #36578

Closed
kstenerud opened this issue Jan 15, 2020 · 3 comments
Closed

^0 is interpreted as -1 #36578

kstenerud opened this issue Jan 15, 2020 · 3 comments

Comments

@kstenerud
Copy link

What did you do?

https://play.golang.org/p/2kMDzm1vAtF

package main

import (
	"fmt"
)

func main() {
	var x uint64 = ^0
	fmt.Println(x)
}

What did you expect to see?

I expected it to print the highest uint64 value possible

What did you see instead?

./prog.go:8:17: constant -1 overflows uint64

The logical complement of 0 should not be assumed to be signed.

@bradfitz
Copy link
Contributor

@randall77
Copy link
Contributor

Use x := ^uint64(0) instead.

@minux
Copy link
Member

minux commented Jan 15, 2020

Need to use ^uint64(0) as explained https://blog.golang.org/constants#TOC_11%2e

@minux minux closed this as completed Jan 15, 2020
@golang golang locked and limited conversation to collaborators Jan 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants