Hamming Code (7,4) - C++ Implementation Failure -
#include <iostream> #include <string> using namespace std; int get_bin_representation(char x){ if(x == '1'){ homecoming 1; } else if(x == '0'){ homecoming 0; } } int gen_hamming_code(string token){ int bits[4]; int temp(0); for(int k=0; k<4; k++){ bits[k] = get_bin_representation(token.at(k)); } int ham_code[7]; ham_code[0] = bits[0] + bits[1] + bits[3]; ham_code[1] = bits[0] + bits[2] + bits[3]; ham_code[2] = bits[0]; ham_code[3] = bits[1] + bits[2] + bits[3]; ham_code[4] = bits[1]; ham_code[5] = bits[2]; ham_code[6] = bits[3]; for(int h=0; h<7; h++){ temp = ham_code[h]; ham_code[h] = temp%2; temp = 0; } for(int e=0; e<7; e++){ cout << ham_code[e]; } cout << endl; homecoming 0; } int main(){ string usr_input; string msg; int index(0); cout << "hamming code program" << endl; while(true){ cout << endl << ": "; getline(cin, usr_input); if(usr_input.find("gen") != std::string::npos){ for(int i=0; i<usr_input.length(); i++){ if(usr_input.at(i) == ' '){ index = i; } } for(int j=index; j<usr_input.length(); j++){ msg+=usr_input.at(j); } cout << "hamming code (7,4): "; gen_hamming_code(msg); } } }
i used linear algebra definition supplied wikipedia ('hamming code (7,4)'). @ several points in program, printed variable contents, fixing 1 problem lead another. verify if output correct, compared illustration available on wikipedia, , result produced online calculator.
updated: question resolved. used adaptation of algorithm provided here (without amp).
well, wrong:
ham_code[0] = bits[0] + bits[1] + bits[3];
hamming codes defined using gf(2) arithmetic. add-on in gf(2) c++ xor operator (^
). utilize right operator, , can away later %2
loop.
you've intermingled parity bits plaintext, never done when learned it. well, online simulator using simple order (plaintext, parity) without interleaving.
c++ hamming-code
No comments:
Post a Comment