class Solution { public: bool hasAlternatingBits(int n) { int last = -1; while (n) { int x = n & 1; if (last == -1) { last = x; } else { if (x == last) { return false; } else { last = x; } } n >>= 1;//n右移1位 } return true; } };