LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-12-2010, 11:09 AM   #1
karishma01
LQ Newbie
 
Registered: Sep 2010
Location: india
Posts: 2

Rep: Reputation: -2
Unhappy mplementing hamming code in c lang


hey will u pls try to help me implementing hamming code in c:

Calculating the Hamming Code
The key to the Hamming Code is the use of extra parity bits to allow the identification of a single error. Create the code word as follows:

Mark all bit positions that are powers of two as parity bits. (positions 1, 2, 4, 8, 16, 32, 64, etc.)
All other bit positions are for the data to be encoded. (positions 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, etc.)
Each parity bit calculates the parity for some of the bits in the code word. The position of the parity bit determines the sequence of bits that it alternately checks and skips.
Position 1: check 1 bit, skip 1 bit, check 1 bit, skip 1 bit, etc. (1,3,5,7,9,11,13,15,...)
Position 2: check 2 bits, skip 2 bits, check 2 bits, skip 2 bits, etc. (2,3,6,7,10,11,14,15,...)
Position 4: check 4 bits, skip 4 bits, check 4 bits, skip 4 bits, etc. (4,5,6,7,12,13,14,15,20,21,22,23,...)
Position 8: check 8 bits, skip 8 bits, check 8 bits, skip 8 bits, etc. (8-15,24-31,40-47,...)
Position 16: check 16 bits, skip 16 bits, check 16 bits, skip 16 bits, etc. (16-31,48-63,80-95,...)
Position 32: check 32 bits, skip 32 bits, check 32 bits, skip 32 bits, etc. (32-63,96-127,160-191,...)
etc.
Set a parity bit to 1 if the total number of ones in the positions it checks is odd. Set a parity bit to 0 if the total number of ones in the positions it checks is even.
Here is an example:

A byte of data: 10011010(ip-user)
c programming for follwing steps:
Create the data word, leaving spaces for the parity bits: _ _ 1 _ 0 0 1 _ 1 0 1 0
Calculate the parity for each parity bit (a ? represents the bit position being set):

Position 1 checks bits 1,3,5,7,9,11:
? _ 1 _ 0 0 1 _ 1 0 1 0. Even parity so set position 1 to a 0: 0 _ 1 _ 0 0 1 _ 1 0 1 0
Position 2 checks bits 2,3,6,7,10,11:
0 ? 1 _ 0 0 1 _ 1 0 1 0. Odd parity so set position 2 to a 1: 0 1 1 _ 0 0 1 _ 1 0 1 0
Position 4 checks bits 4,5,6,7,12:
0 1 1 ? 0 0 1 _ 1 0 1 0. Odd parity so set position 4 to a 1: 0 1 1 1 0 0 1 _ 1 0 1 0
Position 8 checks bits 8,9,10,11,12:
0 1 1 1 0 0 1 ? 1 0 1 0. Even parity so set position 8 to a 0: 0 1 1 1 0 0 1 0 1 0 1 0
Code word: 011100101010. (output)
 
Old 09-12-2010, 11:57 AM   #2
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,245

Rep: Reputation: 1299Reputation: 1299Reputation: 1299Reputation: 1299Reputation: 1299Reputation: 1299Reputation: 1299Reputation: 1299Reputation: 1299
So you copied out your homework question word-for-word into a forum post. What do you expect us to do with that?

Narrow down what you need help with. How do you do that? Well, first you do the homework. If you end up with a solution that doesn't quite work, then you'll be able to post here about the part that's not working.

Last edited by dugan; 09-12-2010 at 12:17 PM.
 
1 members found this post helpful.
Old 11-26-2010, 05:54 AM   #3
AcEg33k
LQ Newbie
 
Registered: Jan 2010
Distribution: Slackware
Posts: 7

Rep: Reputation: 1
Giving you simple implementation. But seriously , Try it yourself. Google is your friend and wiki is your counseller.
//hamminggen.c
#include<stdio.h>
void main()
{
int d[4],c[7],i;
printf("\n Enter the 4 bit date seperated by blanks\n");
for(i=0;i<4;i++)
scanf("%d",&d[i]);
for(i=0;i<4;i++)
c[i]=d[i];
c[4]=(d[0]+d[2]+d[3])%2;
c[5]=(d[0]+d[1]+d[3])%2;
c[6]=(d[1]+d[2]+d[3])%2;
printf("\n The data bit appended with correction bit is \n");
for(i=0;i<7;i++)
printf("%d",c[i]);
}





// hammingdetection.c
#include<stdio.h>
void main()
{
int r[7],s[3],index,i,j,sum;
int h[3][7]={{1,0,1,1,1,0,0},{1,1,0,1,0,1,0},{0,1,1,1,0,0,1}};
printf("\n Enter the 7 bit information: \n");
for(i=0;i<7;i++)
scanf("%d",&r[i]);
for(j=0;j<3;j++)
{
sum=0;
for(i=0;i<7;i++)
sum+=h[i][j]*r[i];
sum=sum%2;
s[j]=sum;
}
if(s[0]==0 && s[1]==0 && s[2]==0)
{
printf("\n Error free Implementation.\n");
printf("\n The data bits are\n");
for(i=0;i<4;i++)
printf("%d",r[i]);
exit(0);
}
for(j=0;j<7;j++)
{
if(s[0]==h[0][j] && s[1]==h[1][j] && s[2]==h[2][j])
{
index=j;
break;
}
}
printf("\n The error is in bit no %d\n",index+1);
if(r[index]==0)
r[index]=1;
else
r[index]=0;
printf("\n The correct information is :");
for(i=0;i<7;i++)
printf("%d",r[i]);
printf("\n The data bits are : ");
for(i=0;i<4;i++)
printf("%d",r[i]);
}
 
  


Reply

Tags
code, implementing, lang


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/Signature canonas Linux - Software 3 06-16-2008 04:00 AM
What is the use of lang in <head lang=''> tooparam Programming 2 02-01-2006 05:55 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
LANG/ LC_ALL variables on Suse not set.. code segfaults nkshirsagar Suse/Novell 0 01-04-2005 05:06 AM


All times are GMT -5. The time now is 08:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration