Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luojiyin1987/19d64c06e5f23a45c2d8144f62d2dd65 to your computer and use it in GitHub Desktop.
Save luojiyin1987/19d64c06e5f23a45c2d8144f62d2dd65 to your computer and use it in GitHub Desktop.
Number Complement
func findComplement(num int) int {
u := uint32(num)
mask := uint32(0xffffffff)
for u & mask != 0 {
mask <<= 1
}
return int(^(u | mask))
}
@luojiyin1987
Copy link
Author

func findComplement(num int) int {
if num == 0 {
return 1
}
i := 1
for num >= i {
num ^= i
i <<= 1
}
return num
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment