不知道爲什麼先前這種通不過
class Solution {
public:
int hammingDistance(int x, int y) {
int answer = 0;
int tmp = x^y;
int count = 31;
while(count--){
// if(x&1 != y&1){
// answer += 1;
// }
// x >>= 1;
// y >>= 1;
if(tmp&1 == 1){
answer += 1;
}
tmp >>= 1;
}
return answer;
}
};
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------知道錯誤的原因了,!=優先級是7,按位&優先級是8,所以這樣寫實際上是先做了!=後做了&