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:
Below is the implementation of the above approach:
// 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