package main
import("fmt""strconv")funcNumOfSetBits(n int)int{
count :=0for n !=0{
count += n &1
n >>=1}return count
}funcmain(){
n :=20
fmt.Printf("Binary representation of %d is: %s.\n", n,
strconv.FormatInt(int64(n),2))
fmt.Printf("The total number of set bits in %d is %d.\n", n,NumOfSetBits(n))}
Go
输出
Binary representation of 20 is:10100.
The total number of set bits in 20 is 2.