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

&nbsp;
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.

&nbsp;
Constraints:


	1 &lt;= n &lt;= 231 - 1


class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
        bin_n = bin(n)[2:]
        for i in range(len(bin_n)-1):
            if bin_n[i] == '0' and bin_n[i+1] == '0':
                return False
            
            if bin_n[i] == '1' and bin_n[i+1] == '1':
                return False
            
        return True
class Solution {
    public boolean hasAlternatingBits(int n) {
        int flag = 1;
        if(n % 2 == 0) flag = 0;
        return bin(n / 2, flag);
    }
    public boolean bin(int n, int flag) {
        if(flag == n % 2) return false;
        if(n == 0) return true;
        else return bin(n / 2, n % 2);
    }
}
class Solution {
public:
    const static uint32_t a = 0b10101010101010101010101010101010;
    bool hasAlternatingBits(int n) {
        return ((a >> __builtin_clz(n)) ^ n) == 0;
    }
};
/**
 * @param {number} n
 * @return {boolean}
 */
var hasAlternatingBits = function(n) {
  let previous;
  while (n) {
    const current = n & 1;
    if (previous === current) return false;
    previous = current;
    n >>>= 1;
  }
  return true;
};
Binary Number with Alternating Bits