package main
import "fmt"
func isPowerOfTwo(n int) bool {
count := 0
for n != 0 {
count += 1
n &= n-1
}
return count == 1
}
func main() {
fmt.Println(isPowerOfTwo(218))
}
程序输出如下,

image.png
package main
import "fmt"
func isPowerOfTwo(n int) bool {
count := 0
for n != 0 {
count += 1
n &= n-1
}
return count == 1
}
func main() {
fmt.Println(isPowerOfTwo(218))
}
程序输出如下,