【面试题】计算某个整数 1 的位数,即 Hamming weight

polaris · · 2871 次点击
```go func hammingWeight(n uint32) int { var w int for { if n == 0 { break } if n&1 == 1 { w++ } n = n >> 1 } return w } ```
#1
更多评论
func hammingWeight(n uint32) int { str := strconv.FormatInt(int64(n), 10) v := strings.Count(str, "1") return v }
#2
`func hammingWeight(n uint32) int { str := strconv.FormatInt(int64(n), 10) v := strings.Count(str, "1") return v }`
#3