From 069b121a26d0b1ca1ad43a747d441b5882cdb405 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 2 Mar 2006 01:27:57 +0000 Subject: [PATCH] Don't hardcode [4] for the extra bit; use a #define instead. --- hamming.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/hamming.c b/hamming.c index 215b1dd..33376f3 100644 --- a/hamming.c +++ b/hamming.c @@ -2,6 +2,7 @@ #define DATA_BITS 11 #define PARITY_BITS 5 +#define EXTRA_BIT_POSITION (PARITY_BITS - 1) #define CODE_BITS (DATA_BITS + PARITY_BITS) #define NUM_DATA_WORDS (1 << DATA_BITS) @@ -22,20 +23,20 @@ unsigned generate_parity(unsigned data) unsigned parity[PARITY_BITS]; unsigned i; - parity[4] = 0; + parity[EXTRA_BIT_POSITION] = 0; for (i = 0; i < DATA_BITS; ++i) { bits[i] = (data & (1 << i)) ? 1 : 0; - parity[4] ^= bits[i]; + parity[EXTRA_BIT_POSITION] ^= bits[i]; } parity[0] = bits[0] ^ bits[1] ^ bits[3] ^ bits[4] ^ bits[6] ^ bits[8] ^ bits[10]; parity[1] = bits[0] ^ bits[2] ^ bits[3] ^ bits[5] ^ bits[6] ^ bits[9] ^ bits[10]; parity[2] = bits[1] ^ bits[2] ^ bits[3] ^ bits[7] ^ bits[8] ^ bits[9] ^ bits[10]; parity[3] = bits[4] ^ bits[5] ^ bits[6] ^ bits[7] ^ bits[8] ^ bits[9] ^ bits[10]; - parity[4] ^= parity[0] ^ parity[1] ^ parity[2] ^ parity[3]; + parity[EXTRA_BIT_POSITION] ^= parity[0] ^ parity[1] ^ parity[2] ^ parity[3]; - return parity[4] | (parity[3] << 1) | (parity[2] << 2) | (parity[1] << 3) | (parity[0] << 4); + return parity[EXTRA_BIT_POSITION] | (parity[3] << 1) | (parity[2] << 2) | (parity[1] << 3) | (parity[0] << 4); } unsigned make_codeword(unsigned data) @@ -103,13 +104,13 @@ unsigned correct_single_bit_error(unsigned code) unsigned parity[PARITY_BITS]; unsigned i, bp = 0; - parity[4] = 0; + parity[EXTRA_BIT_POSITION] = 0; for (i = 0; i < CODE_BITS; ++i) { bits[i] = (code & (1 << i)) ? 1 : 0; } for (i = 1; i < CODE_BITS; ++i) { - parity[4] ^= bits[i]; + parity[EXTRA_BIT_POSITION] ^= bits[i]; } parity[0] = bits[PARITY_BITS+0] ^ bits[PARITY_BITS+1] ^ bits[PARITY_BITS+3] ^ bits[PARITY_BITS+4] ^ bits[PARITY_BITS+6] ^ bits[PARITY_BITS+8] ^ bits[PARITY_BITS+10]; @@ -126,11 +127,11 @@ unsigned correct_single_bit_error(unsigned code) if (bp != 0) { /* flip the wrong bit */ code ^= (1 << permutation_table[bp]); - parity[4] ^= 1; + parity[EXTRA_BIT_POSITION] ^= 1; } /* recompute the lower parity */ - return (code & ~1) | parity[4]; + return (code & ~1) | parity[EXTRA_BIT_POSITION]; } void check_zero_bit_detection() -- 2.39.2