```
class Solution {
public:
bool hasAlternatingBits(int n) {
int lb;
lb = n&1;
n = n>>1;
while(n>0){
if((n & 1) == 1){
if(lb == 1)
return false;
lb = 1;
}
else{
if(lb == 0)
return false;
lb = 0;
}
n = n>>1;
}
return true;
}
};
```