JuniorBit Manipulation
Binary Number with Alternating Bits
Given a positive integer n, check whether it has alternating bits: namely, whether every pair of adjacent bits in its binary representation always has different values.
Return true if n has alternating bits, otherwise return false.
Example 1
Input
n = 5Output
trueThe binary representation of 5 is 101, whose adjacent bits are all different.
Example 2
Input
n = 7Output
falseThe binary representation of 7 is 111, which has adjacent bits with the same value.
Constraints
- 1 <= n <= 2^31 - 1