Leetcode 693 - Binary Number with Alternating Bits

Note:

  • Use & 1 to get the least significant bit.
  • Use lastBit to preserve last bit.
  • Note that & has a lower priority than ===.

Question:

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.

Example:

1
2
3
Input: n = 5
Output: true
Explanation: The binary representation of 5 is: 101

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @param {number} n
* @return {boolean}
*/
var hasAlternatingBits = function(n) {
let lastBit = n & 1;
while (n !== 0) {
n = n >>> 1;
// Has to add () to n & 1.
if ((n & 1) === lastBit) return false;
lastBit = n & 1;
}
return true;
};