Search code examples
c++mathhashhamming-distance

How should I store and compute Hamming distance between binary codes?


  1. How can I efficiently store binary codes? For certain fixed sizes, such as 32 bits, there are primitive types that can be used. But what if I my binary codes are much longer?

  2. What is the fastest way to compute the Hamming distance between two binary codes?


Solution

    1. Use std::bitset<N>, defined in the <bitset> header, where N is the number of bits (not bytes).
    2. Compute the Hamming distance between two binary codes a and b using (a ^ b).count().