Given a message bit in the form of an array msgBit[], the task is to find the Hamming Code of the given message bit.

Examples:

Input:_ S = “0101”_

Output:

Generated codeword:

r1 r2 m1 r4 m2 m3 m4

0 1 0  0 1  0  1

Explanation:

Initially r1, r2, r4 is set to ‘0’.

r1 = Bitwise XOR of all bits position that has ‘1’ in its 0th-bit position.

r2 = Bitwise XOR of all bits that has ‘1’ in its 1st-bit position.

r3 = Bitwise XOR of all bits that has ‘1’ in its 2nd-bit position.

Input:_ S = “0111”_

Output:

_Generated codeword: _

_r1 r2 m1 r4 m2 m3 m4 _

_0 0 0  1 1  1  1 _

Approach: The idea is to first find the number of redundant bits which can be found by initializing **r **with **1 **and then incrementing it by 1 each time while 2r is smaller than (m + r + 1) where **m **is the number of bits in the input message. Follow the below steps to solve the problem:

  • Initialize **r **by **1 **and increment it by **1 **until 2r is smaller than m+r+1.
  • Initialize a vector **hammingCode **of size r + m which will be the length of the output message.
  • Initialize all the positions of redundant bits with -1 by traversing from i = 0 to r – 1 and setting hammingCode [2i – 1] = -1. Then place the input message bits in all the positions where hammingCode[j] is not -1 in order where 0 <= j < (r + m).
  • Initialize a variable **one_count **with **0 **to store the number of ones and then traverse from i = 0 to (r + m – 1).
  • If the current bit i.e., hammingCode[i] is not -1 then find the message bit containing set bit at log2(i+1)th position by traversing from j = i+2 to r+m by incrementing one_count by 1 if (j & (1<<x)) is not 0 and hammingCode[j – 1] is 1.
  • If for index i, **one_count **is even, set hammingCode[i] = 0 otherwise set hammingCode[i] = 1.
  • After traversing, print the **hammingCode[] **vector as the output message.

Below is the implementation of the above approach:

  • C
  • C++

// C program for the above approach

#include <math.h>

#include <stdio.h>

// Store input bits

**int** input[32];

// Store hamming code

**int** code[32];

**int** ham_calc(``**int**``, **int**``);

**void** solve(``**int** input[], **int**``);

// Function to calculate bit for

// ith position

**int** ham_calc(``**int** position, **int** c_l)

{

**int** count = 0, i, j;

i = position - 1;

// Traverse to store Hamming Code

**while** (i < c_l) {

**for** (j = i; j < i + position; j++) {

// If current boit is 1

**if** (code[j] == 1)

count++;

}

// Update i

i = i + 2 * position;

}

**if** (count % 2 == 0)

**return** 0;

**else**

**return** 1;

}

// Function to calculate hamming code

**void** solve(``**int** input[], **int** n)

{

**int** i, p_n = 0, c_l, j, k;

i = 0;

// Find msg bits having set bit

// at x'th position of number

**while** (n > (``**int**``)``**pow**``(2, i) - (i + 1)) {

p_n++;

i++;

}

c_l = p_n + n;

j = k = 0;

// Traverse the msgBits

**for** (i = 0; i < c_l; i++) {

// Update the code

**if** (i == ((``**int**``)``**pow**``(2, k) - 1)) {

code[i] = 0;

k++;

}

// Update the code[i] to the

// input character at index j

**else** {

code[i] = input[j];

j++;

}

}

// Traverse and update the

// hamming code

**for** (i = 0; i < p_n; i++) {

// Find current position

**int** position = (``**int**``)``**pow**``(2, i);

// Find value at current position

**int** value = ham_calc(position, c_l);

// Update the code

code[position - 1] = value;

}

// Print the Hamming Code

**printf**``(``"\nThe generated Code Word is: "``);

**for** (i = 0; i < c_l; i++) {

**printf**``(``"%d"``, code[i]);

}

}

// Driver Code

**void** main()

{

// Given input message Bit

input[0] = 0;

input[1] = 1;

input[2] = 1;

input[3] = 1;

**int** N = 4;

// Function Call

solve(input, N);

}

Output:

The generated Code Word is: 0001111

Time Complexity:_ O((M + R)2) where M is the number of bits in the input message and R is the number of redundant bits _

Auxiliary Space:_ O(M + R)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#bit magic #c programs #c++ programs #computer networks #strings #bit #maths-power

Hamming code Implementation in C/C++
63.65 GEEK