class Solution {

public boolean hasAlternatingBits(int n) {

// Save the first bit in previous bit and keep checking if XOR of previous bit and current bit equal to 1 then continue else return false

```
int previousBit = (n)&1;
n=n>>1;
while(n>0)
{
if(((n&1)^previousBit)==0)
return false;
previousBit = n&1;
n=n>>1;
}
return true;
}
```

}