go: add «476. Number Complement»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-22 10:49:47 +02:00
parent a3fca26cdd
commit c34927bd88
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

11
go/number-complement.go Normal file
View file

@ -0,0 +1,11 @@
package main
func findComplement(num int) int {
mask := 0
for tmp := num; tmp != 0; tmp >>= 1 {
mask <<= 1
mask |= 1
}
return num ^ mask
}