Skip to content

Instantly share code, notes, and snippets.

@caelifer
Last active September 14, 2016 20:26
Show Gist options
  • Save caelifer/e25024082d565db9b446 to your computer and use it in GitHub Desktop.
Save caelifer/e25024082d565db9b446 to your computer and use it in GitHub Desktop.
package main
import "fmt"
var bits [256]int
func init() {
for i := range bits {
bits[i] = bits[i>>1] + i&0x1
}
}
func main() {
var a, b [32]byte
a[0], a[31] = 1, 1
fmt.Println(countDiffBits(a, b))
}
func countDiffBits(a, b [32]byte) (res int) {
for i := 0; i < 32; i++ {
res += bits[int(a[i]^b[i])]
}
return
}
@caelifer
Copy link
Author

caelifer commented Nov 18, 2015

Live code - https://play.golang.org/p/O7ZOe_3Emy

Output:

2

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