Created
July 24, 2017 15:04
-
-
Save luojiyin1987/19d64c06e5f23a45c2d8144f62d2dd65 to your computer and use it in GitHub Desktop.
Number Complement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func findComplement(num int) int { | |
| u := uint32(num) | |
| mask := uint32(0xffffffff) | |
| for u & mask != 0 { | |
| mask <<= 1 | |
| } | |
| return int(^(u | mask)) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
func findComplement(num int) int {
if num == 0 {
return 1
}
i := 1
for num >= i {
num ^= i
i <<= 1
}
return num
}