text: Hamming code Implementation in C / C ++ | C program for the above approach ; Store input bits ; Store hamming code ; Function to calculate bit for ith position ; Traverse to store Hamming Code ; If current boit is 1 ; Update i ; Function to calculate hamming code ; Find msg bits having set bit at x 'th position of number ; Traverse the msgBits ; Update the code ; Update the code [ i ] to the input character at index j ; Traverse and update the hamming code ; Find current position ; Find value at current position ; Update the code ; Print the Hamming Code ; Driver Code ; Given input message Bit ; Function Call
code: #include <math.h> NEW_LINE #include <stdio.h> NEW_LINE int input [ 32 ] ; int code [ 32 ] ; int ham_calc ( int , int ) ; void solve ( int input [ ] , int ) ; int ham_calc ( int position , int c_l ) { int count = 0 , i , j ; i = position - 1 ; while ( i < c_l ) { for ( j = i ; j < i + position ; j ++ ) { if ( code [ j ] == 1 ) count ++ ; } i = i + 2 * position ; } if ( count % 2 == 0 ) return 0 ; else return 1 ; } void solve ( int input [ ] , int n ) { int i , p_n = 0 , c_l , j , k ; i = 0 ; while ( n > ( int ) pow ( 2 , i ) - ( i + 1 ) ) { p_n ++ ; i ++ ; } c_l = p_n + n ; j = k = 0 ; for ( i = 0 ; i < c_l ; i ++ ) { if ( i == ( ( int ) pow ( 2 , k ) - 1 ) ) { code [ i ] = 0 ; k ++ ; } else { code [ i ] = input [ j ] ; j ++ ; } } for ( i = 0 ; i < p_n ; i ++ ) { int position = ( int ) pow ( 2 , i ) ; int value = ham_calc ( position , c_l ) ; code [ position - 1 ] = value ; } printf ( " The generated Code Word is : " for ( i = 0 ; i < c_l ; i ++ ) { printf ( " % d " , code [ i ] ) ; } } void main ( ) { input [ 0 ] = 0 ; input [ 1 ] = 1 ; input [ 2 ] = 1 ; input [ 3 ] = 1 ; int N = 4 ; solve ( input , N ) ; }