3
  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?

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
mrgloom
  • 20,061
  • 36
  • 171
  • 301

1 Answers1

6
  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().
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
  • I don't understand what if use 30 bit codes I still can use 32bit integer?(maybe mask 2 first bits somehow)? – mrgloom Oct 02 '14 at 20:36
  • @mrgloom I would use this approach, profile it, and then only if you determine it is too slow look at doing something custom. My understanding is that a `std::bitset<30>` will only use 4 bytes of storage. – Timothy Shields Oct 02 '14 at 20:41
  • Nice Timothy, maybe you can help with [XOR bitset when 2D bitset is stored as 1D](http://stackoverflow.com/questions/40787731/xor-bitset-when-2d-bitset-is-stored-as-1d). – gsamaras Nov 24 '16 at 13:32