Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luojiyin1987/6d90362013e080e23e41a60d6516aec7 to your computer and use it in GitHub Desktop.
Save luojiyin1987/6d90362013e080e23e41a60d6516aec7 to your computer and use it in GitHub Desktop.
Binary Number with Alternating Bits
func hasAlternatingBits(n int) bool {
count := 0
bit1 := 3
bit2 := 2
for n > 0 {
if count== 0 {
bit1 = n &1
fmt.Println(bit1, bit2)
} else {
bit1, bit2 = (n &1) , bit1
fmt.Println(bit1, bit2)
}
if bit1 == bit2 {
return false
}
n >>=1
count ++
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment