Created
July 24, 2018 02:01
-
-
Save luojiyin1987/6d90362013e080e23e41a60d6516aec7 to your computer and use it in GitHub Desktop.
Binary Number with Alternating Bits
This file contains hidden or 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
| 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