aboutsummaryrefslogtreecommitdiff
path: root/hamming_distance.c
blob: 625f17891da67259cb34b814f72bf0d885077db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 461. Hamming Distance
#include "leetcode.h"

/*
 * the hamming distance between two integers is the number of positions at which
 * the corresponding bits are different. given two integers 'x' and 'y', return
 * the hamming distance between them.
 */

int hammingDistance(int x, int y) {
  int ans = 0, n = x ^ y;
  while (n) {
    ++ans;
    n &= n - 1;
  }
  return ans;
}

int main() {
  printf("%d\n", hammingDistance(1, 4)); // expect: 2
  printf("%d\n", hammingDistance(3, 1)); // expect: 1
}