Binary Number with Alternating Bits

Given a positive integer n, return true if its binary representation has alternating bits, such as 1010 or 0101. Pattern focus: Check kth Bit. Compare adjacent bits and ensure no two neighboring bits are equal.

Input Format

n = positive integer

Output Format

true if the binary representation has alternating bits

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • n must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

n = 5

Output:

true

Explanation:

5 is 101 in binary, which alternates.

Example 2:

Input:

n = 7

Output:

false

Explanation:

7 is 111 in binary, which has repeated bits.

Loading...
Binary Number with Alternating Bits