Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
Example
Input: 5
Output: True
Explanation:
The binary representation of 5 is: 101
Input: 7
Output: False
Explanation:
The binary representation of 7 is: 111.
Solution
class Solution(object):
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
while n>0:
flag = n%2
n = n/2
if(flag == n%2):
return False
return True