Pre-requisite: Hamming Code
Given a message bit in the form of an array msgBit, the task is to find the Hamming Code of the given message bit.
Input: S = “0101”
r1 r2 m1 r4 m2 m3 m4
0 1 0 0 1 0 1
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”
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:
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.
- Generate string with Hamming Distance as half of the hamming distance between strings A and B
- Hamming code Implementation in Java
- Hamming Code in Computer Network
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Reduce Hamming distance by swapping two characters
- Hamming distance between two Integers
- Sum of Hamming difference of consecutive numbers from 0 to N | Set 2
- Hamming Distance between two strings
- Morse Code Implementation
- Print 12 Days Of Christmas in a few lines of code | The xmas.c code
- Difference between Fastag, Bar Code, QR Code and NFC
- Difference between QR Code, Bar Code and NFC
- Implementation of Affine Cipher
- Pattern Occurrences : Stack Implementation Java
- Implementation of Diffie-Hellman Algorithm
- TCP Server-Client implementation in C
- UDP Server-Client implementation in C
- UDP Client Server using connect | C implementation
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.