# Decoding bit streams using Hamming Code

I want to decode the following bit stream using Hamming code: 01110110

Once using even parity and once odd parity. I will show my work. For even parity:

$$p_{1} \; p_{2} \; 0 \; p_{3} \; 1 \; 1 \; 1 \; p_{4} \; 0 \; 1 \; \; 1 \; 0$$

Now, we check:

$$p_{1}: p_{1} \; 0 \; 1 \; 1 \; 0 \; 1$$, since there is an odd number of 1's, $$p_{1}=1$$

$$p_{2}: p_{2} \; 0 \; 1 \; 1 \; 1 \; 1$$, since there is an even number of 1's, $$p_{2}=0$$

$$p_{4}: p_{3} \; 1 \; 1 \; 1 \;0$$, since there is an odd number of 1's, $$p_{3}=1$$

$$p_{8}: p_{4} \; 0 \; 1 \; 1 \;0$$, since there is an even number of 1's, $$p_{4}=0$$

so the following hamming code is $$1 \; 0 \; 0 \; 1\; 1 \; 1 \; 1 \; 0 \; 0 \; 1 \; \; 1 \; 0$$

As for odd, the parities will just be the opposite and the code will be

$$0\; 1 \; 0 \; 0 \; 1 \; 1 \; 1 \; 1 \; 0 \; 1 \; \; 1 \; 0$$

Can someone tell me if my work is correct? Because I am a bit confused and would appreciate the help.

• Some background is missing here. What does it mean to use "even parity" or "odd parity"? What is the Hamming code for you? – Yuval Filmus Nov 4 '19 at 11:39

There are many equivalent Hamming codes depending on the ordering of parity and data bits. Assuming that:

• You are using the specific bit ordering described here: https://en.wikipedia.org/wiki/Hamming_code#General_algorithm
• By "even parity" you mean that the parity bits are computed as the sum (modulo 2) of the corresponding groups of bits
• By "odd parity" you mean that the parity bits are computed as 1 minus the sum (modulo 2) of the corresponding groups of bits,

then yes, your simulation of the algorithm is correct.

I'm not really understanding some parts of your question, The Hamming algorithm is used to count the number of 1 bits of a binary number. The idea is to create a mask = 1 and check for every of the 32 bits if (mask & n) == 1, then shift the mask to the left by one. If true is returned, it means that the binary representation of n has a 1 at that position, so increase the bit number. Here is an example:

int hamming(int n) {
int bits = 0, mask = 1;
for (int i = 0; i < 32; i++) {
if ((n & mask) != 0) bits++;
mask <<= 1; //shift left by one
}
return bits;
}