#693
Binary Number with Alternating Bits
newbie · 290 · lc easy +20 · verified · 69.8% accepted · 1,724 likes · top 78%
Description
Given a positive integer, determine whether every pair of adjacent bits in its binary representation alternates between 0 and 1. Return true if so, otherwise false.
Example 1:
Input: n = 5
Output: true
Explanation: The binary representation of 5 is: 101
Example 2:
Input: n = 7
Output: false
Explanation: The binary representation of 7 is: 111.
Example 3:
Input: n = 11
Output: false
Explanation: The binary representation of 11 is: 1011.
Code
1
2
3