Skip to content

Instantly share code, notes, and snippets.

@udhos
Created March 7, 2019 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udhos/923078025f9908f16bacbf98c2e99e8b to your computer and use it in GitHub Desktop.
Save udhos/923078025f9908f16bacbf98c2e99e8b to your computer and use it in GitHub Desktop.
bool2int golang unsafe trick
// https://stackoverflow.com/a/51097467/1011695
package main
import (
"fmt"
"unsafe"
)
func main() {
fmt.Println(bool2int(true))
fmt.Println(bool2int(false))
}
func bool2int(a bool) uint64 {
return *(*uint64)(unsafe.Pointer(&a))&1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment