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?
What is the fastest way to compute the Hamming distance between two binary codes?
How should I store and compute Hamming distance between binary codes?
Asked Answered
What about std::bitset? –
Monopolist
- Use
std::bitset<N>
, defined in the<bitset>
header, whereN
is the number of bits (not bytes). - Compute the Hamming distance between two binary codes
a
andb
using(a ^ b).count()
.
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.