This is my CPP solution.
class Solution {
private:
string toBinary(int n){
string res;
while(n){
int bit = n%2;
res.push_back(bit + '0');
n/=2;
}
reverse(res.begin(),res.end());
return res;
}
public:
bool hasAlternatingBits(int n) {
string bin = toBinary(n);
n = bin.size();
for(int i=0;i<n-1;i++)
if(bin[i]==bin[i+1])
return false;
return true;
}
};