int countOnesBits(int x) { int result = 0; while (x > 0) { if (x % 2 == 1) { result++; } x = x >> 1; } return result; } int hammingDistance(int x, int y) { return countOnesBits(x ^ y); }