congr / world

2 stars 1 forks source link

LeetCode : 461. Hamming Distance #512

Closed congr closed 5 years ago

congr commented 5 years ago

image

congr commented 5 years ago
class Solution {
    public int hammingDistance(int x, int y) {
        int n = x ^ y;
        int cnt = 0;
        for (int i = 0; i < 32; i++) cnt += (n >> i) & 1;
        return cnt;
    }
}
congr commented 5 years ago
class Solution {
    public int hammingDistance(int x, int y) {
        return Integer.bitCount(x ^ y);
    }
}