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

1

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?

Benzofuran answered 2/10, 2014 at 20:4 Comment(1)
What about std::bitset?Monopolist
T
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().
Twoseater answered 2/10, 2014 at 20:12 Comment(3)
I don't understand what if use 30 bit codes I still can use 32bit integer?(maybe mask 2 first bits somehow)?Benzofuran
@Benzofuran 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.Twoseater
Nice Timothy, maybe you can help with XOR bitset when 2D bitset is stored as 1D.Wooten

© 2022 - 2024 — McMap. All rights reserved.