Last active
September 14, 2016 20:26
-
-
Save caelifer/e25024082d565db9b446 to your computer and use it in GitHub Desktop.
This file contains 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live code - https://play.golang.org/p/O7ZOe_3Emy
Output: