meaning of the title
Given a positive integer n, determine whether the adjacent digits of n in binary are different. For example, the binary bit of 5 is 101, and the adjacent bits are not the same.
ideas
Since the adjacent bits are not the same, then after shifting one bit to the right and then XORing with n, the result will be all 1s.
class Solution {
public boolean hasAlternatingBits(int n) {
int z = (n>>1)^n;
while (z > 0) {
if ((z&1) != 1) {
return false;
}
z >>= 1;
}
return true;
}
}