How does this bitwise hamming(31,26) encoder work in C? (Bitmasking)

Viewed 150

I understand what the individual operations are (such as the bitwise ANDs an ORs), but I don't understand why they have been used where they have been.

Also, my understanding is that the first set of masks are used to compute parity bits. But I don't understand why the 2nd set of masks have been chosen or what their purpose is. Can anyone help me get my head around this please?

rawData is the input word that is to be encoded by the hamming.c function.

1 Answers

Doing the encoding of a [31,26] Hamming code, the function hammingEncode() encodes a message rawData consisting of 26 message bits by inserting 5 parity bits on positions 0, 1, 3, 7 and 15 (counting bits starting from 0).

The purpose of the part you are asking about:

  unsigned int mask1 = 0b11111111111111100000000000;
  unsigned int mask2 = 0b00000000000000011111110000;
  unsigned int mask3 = 0b00000000000000000000001110;
  unsigned int mask4 = 0b00000000000000000000000001;

  encodedData |= (rawData & mask1) << 5;
  encodedData |= (rawData & mask2) << 4;
  encodedData |= (rawData & mask3) << 3;
  encodedData |= (rawData & mask4) << 2;

is to move the 26 message bits into the correct positions: 16-30, 8-14, 4-6 and 2 using mask1, mask2, mask3 and mask4 respectively.

After that, the parity bits are calculated and inserted on their correct positions.

Related