1319. Binary Number with Alternating Bits

EasyBit ManipulationBit ManipulationXORPattern

Given a positive integer n, return true if its binary representation has strictly alternating bits (no two adjacent bits are equal), otherwise false.

Input: A JSON object {"n": <positive integer>}.

Output: Return true if the bits of n alternate, otherwise false.

Examples

Example 1
Input: {"n":5}
Output: true
Explanation: 101 alternates -> true.
Example 2
Input: {"n":7}
Output: false
Explanation: 111 has adjacent equal bits -> false.

Constraints

Asked by

GoogleMicrosoftAmazon
Solve this problem in the editor →