40 lines
776 B
C++
40 lines
776 B
C++
#include <bitset>
|
|
|
|
template <typename T, int bits>
|
|
struct hamming_code {
|
|
typedef std::bitset<(1 << bits)> value_type;
|
|
|
|
static inline int block_length() {
|
|
return (1 << bits) - 1;
|
|
}
|
|
|
|
static inline int message_length() {
|
|
return (1 << bits) - bits - 1;
|
|
}
|
|
|
|
static inline bool parity(value_type b, int power)
|
|
{
|
|
T mask = 1 << power;
|
|
int sum = 0;
|
|
for (int bit_ix = 0; bit_ix < block_length(); bit_ix++) {
|
|
if (!(bit_ix & mask)) continue;
|
|
sum += b[ix];
|
|
}
|
|
return sum & 1;
|
|
}
|
|
|
|
static inline bool extended_parity(value_type b)
|
|
{
|
|
int sum = 0;
|
|
for (int bit_ix = 0; bit_ix < block_length(); bit_ix++) {
|
|
sum += b[ix];
|
|
}
|
|
return sum & 1;
|
|
}
|
|
|
|
static inline value_type encode(T t)
|
|
{
|
|
value_type ba(t);
|
|
}
|
|
}
|