basics · intermediate · ~15 min
Count differing bit positions between two values.
Implement:
int hamming_distance(unsigned a, unsigned b);
Return the number of bit positions where a and b differ.
Two values a and b.
The count of differing bits (0..32).
Equal values -> 0.
#include <stddef.h>
/* Hamming distance: number of bit positions where a and b differ. */
int hamming_distance(unsigned a,unsigned b){ (void)a;(void)b; return 0; }
Comparing values instead of XOR-ing then counting.
It is symmetric: d(a,b)==d(b,a).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.