Thinking process

check every bit, there are 32 bits in total

be careful with negative integers

public int hammingDistance(int x, int y) {
        int res = 0;
        for (int i = 0; i < 32; i++) {
            if ((x & (1 << i)) != (y & (1 << i))) res++;
        }

        return res;
    }

results matching ""

    No results matching ""