May 1 2014
Hamming Code Implementation in C
Hamming code is a set of error-correction code s that can be used to detect and correct bit errors that can occur when computer data is moved or stored.
Hamming Code Implementation in C
Hamming Code Common Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include<stdio.h> int power(int n,int m) { int i=0; int sum=1; for(i=1;i<=m;i++) sum *= n; return sum; } void dec2bin(char bits[], int l, int a) { int i; for(i=l-1;i>=0;i--) { bits[i] = (a & 1) + '0'; a >>= 1; } } unsigned char chrpos(int ch,int pos) { return (ch & power(2,pos-1))>>(pos-1); } int calcBit(unsigned long c) { int l=0; while(c>0) { l++; c>>=1; } return l; } struct checkbit { unsigned int b:1; }; |
Hamming Code Sender
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | //Hamming Code Sender Example #include<stdio.h> //Standard Input Functions #include<sys/types.h> //Named Pipes (FIFO) Types #include<sys/stat.h> // Named Pipes (FIFO) Functions #include<fcntl.h> //File Control Unix Functions #include<unistd.h> //Unix Standard Functions #include"hamcom.c" int main() { char *myfifo = "myfifo"; //FIFO File Path int fp; //Open Pipe Return Value unsigned char ch;//Original Character char str[]="0000000000000000";//This is not part of Hamming Algorithm just for debug unsigned short cw=0,cand; //Codeword and temp code operation variable int i,ldata,j=0,lcode,rbit=1; //i,j-> Loop variable, ldata-> size of databits // lcode-> size of codeword bits, rbit->size of check bits printf("Creating Pipe...Waiting for receiver for Process...\n"); //Creating Named Pipe //mkfifo(<filepath>,<permission>) if(mkfifo(myfifo,0666) < 0) { perror("FIFO (Named Pipe) could not be created.\n"); exit(-1); } else printf("FIFO (Named Pipe) Created...\n"); //Opening Named Pipe //open(<filepath>,<openingmode>); fp = open(myfifo,O_WRONLY); if(fp < 0) { perror("FIFO (Named Pipe) could not be opened.\n"); exit(-2); } else printf("FIFO (Named Pipe) Opened...\n"); //HAMMING CODE LOGIC STARTS printf("Enter Character : "); scanf("%c",&ch); ldata = calcBit(ch);//Number of bits in original data //Logic of finding number of check bits while(power(2,rbit)<(ldata+rbit+1)) { rbit++; } //Length of codeword = length of data + length of check bits lcode = ldata + rbit; //defining all checkbits struct checkbit r[rbit]; for(i=0;i<rbit;i++) { r[i].b = 0; } //STEP-1 Filling all data bits in codeword for(i=3;i<=lcode;i++) { if(i&(i-1)){ //Condition of Power of 2 cand = ch & power(2,j++); cw = cw | (cand<<(i-j)); } } //codeword after filling data bits dec2bin(str,16,cw); printf("%d %s\n",cw,str); //STEP-2 calculating all checkbits (parity bits) for(i=3;i<=lcode;i++) { if(i & (i-1)){ //condition for Power of 2 for(j=0;j<rbit;j++){ if(i & power(2,j)){ //condition for including data bit in parity or not (1,3,5,7,9... include in 1 because all has 1 in last bit) r[j].b = r[j].b ^ chrpos(cw,i); } } } } //STEP-3 Fill check bits in code word for(i=1,j=0;i<=lcode;i++) { if(!(i&(i-1)))//Conditio nfor only power of 2 { cw = cw | (r[j++].b << (power(2,j)-1)); } } //Final Code word dec2bin(str,16,cw); printf("\n%d %s\n",cw,str); //HAMMING CODE LOGIC ENDS //Writing to Named Pipe //write(<open_pipe_return_value>,<message>,<msgsize>); if(write(fp,&cw,sizeof(cw)) < 0) { perror("Error writing to FIFO (Named Pipe)\n"); exit(-3); } else printf("Message has been sent to FIFO (Named Pipe)\n"); //Closing Named Pipe //close(<open_pipe_return_value>); if(close(fp) < 0) { perror("Error closing FIFO (Named Pipe)\n"); exit(-4); } return 0; } |
Hamming Code Receiver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | //Hamming Code Receiver Example #include<stdio.h> //Standard Input Functions #include<sys/types.h> //Named Pipes (FIFO) Types #include<sys/stat.h> // Named Pipes (FIFO) Functions #include<fcntl.h> //File Control Unix Functions #include<unistd.h> //Unix Standard Functions #include"hamcom.c" //Common function file for hamming code int main() { char *myfifo = "myfifo"; //FIFO File Path int fp; //Open Pipe Return Value unsigned char ch;//Original Character char str[]="0000000000000000";//This is not part of Hamming Algorithm just for debug unsigned short cw=0,cand=0; //Codeword and temp code operation variable int i,ldata,j=0,lcode,rbit=0; //Opening Named Pipe //open(<filepath>,<openingmode>); fp = open(myfifo,O_RDONLY); if(fp < 0) { perror("FIFO (Named Pipe) could not be opened for reading.\n"); exit(-1); } else printf("FIFO (Named Pipe) Opened... Trying to read from\n"); //Reading From Named Pipe //read(<open_pipe_return_value>,<message>,<msgsize>); if(read(fp,&cw,sizeof(cw)) < 0) { perror("Error reading from FIFO (Named Pipe)\n"); exit(-2); } else printf("Message Received from FIFO (Named Pipe)\n"); cw = cw | power(2,9); dec2bin(str,16,cw); printf("%d %s\n",cw,str); //HAMMING CODE LOGIC STARTS lcode = calcBit(cw); printf("%d\n",lcode); for(i=1;i<=lcode;i++) { if(i & (i-1)) continue; rbit++; } struct checkbit r[rbit]; for(i=0;i<rbit;i++) { r[i].b = 0; } for(i=1;i<=lcode;i++) { for(j=0;j<rbit;j++){ if(i & power(2,j)){ r[j].b = r[j].b ^ chrpos(cw,i); } } } for(i=0;i<rbit;i++) { printf(" %d",r[i].b); cand = cand | (r[i].b<<i); } if(cand){ printf("\nError on %d position",cand); printf("\nCorrecting..."); if(chrpos(cw,cand)) cw -= power(2,cand-1); else cw += power(2,cand-1); } printf("\nCORRECT DATA : \n"); dec2bin(str,16,cw); printf("%d %s\n",cw,str); cand = 0; for(i=1,j=0;i<=lcode;i++){ if(i & (i-1)){ cand = cand | (chrpos(cw,i)<<j++); } } printf("%c\n",cand); //HAMMING CODE LOGIC ENDS if(close(fp) < 0) { perror("Error closing FIFO (Named Pipe)\b"); exit(-3); } if(unlink(myfifo) < 0) { perror("Error deleting FIFO (Named Pipe)\b"); exit(-4); } return 0; } |