LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 02-03-2004, 10:13 AM   #1
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Rep: Reputation: 30
haming code & java or C++


Hi everyone,
I am trying to write a project that is supposed to turn a number or character into its binary value, then add redundancy bits to it and corrupt one of the redundancy bits and finally find the corrupt bit and fix it. I am sorry its a little confusing. We are almost done w/ it but i cannot figure out a function to calculate the redundancy bits.
R = redundancy bit

R1 adds bits 1, 3, 5, 7, 9, 11, 13, etc. <~(my approach is to do a for loop, start from 1 and add 2 each time until there are no more bits)

R2 adds bits 2,3,6,7, 10,11, etc. (do a for loop, start from 2 add 4 each time and then go back and start from 3 and add 4 each time until the end of bits) <~~i am not sure if i can do this

R3 adds bits 8,9,10,11,12 (this one is easy).

My question is, do i have to calculate each redundancy bit by itself or is there any other easier way and more efficient way. And calculatings the 2nd redundancy bit seems hard. It looks i have to use two loops.

Thanks in advance.
 
Old 02-03-2004, 11:14 AM   #2
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
There are some ready-to-work CRC (e.g. crc32.h) checksum algorithms (here is one crc32.cpp, and here's the header file: crc32.h.
If you want to implement one, with the mentioned way there seems to be no problem!
Suppose that you want to extract n'th bit from string myStr:
Code:
for (i = 1; i <= size; i++)
{
  R1 += getBit (i * 2 - 1, myStr);

  R2 += getBit (i * 4 + 2, myStr);
  R2 += getBit (i * 4 + 3, myStr);

  R3 += getBit (i + 8, myStr);
}
Now you should just implment `getBit (int bitNum, char* str)'. That's easy:
Code:
// I know this is not a very good implementation (I've done it in hurry;))
int getBit (int bitNum, char* str) {
  int k = bitNum / 8;
  char ch = str[k];
  ch >>= bitNum - k*8;
  ch <<= 8 - (bitNum - k*8);
  return ch;
}

Last edited by Mohsen; 02-03-2004 at 11:25 AM.
 
Old 02-03-2004, 11:29 AM   #3
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
also you should know that this:
Code:
for (int i = 0; i < 100; i++) {
   f1 ();
   f2 ();
}
hase the same order (O(n) where n is 100 here) of running time with:
Code:
for (int i = 0; i < 100; i++)
   f1 ();
for (int j = 0; j < 100; j++)
   f2 ();
 
Old 02-03-2004, 03:35 PM   #4
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
Thanks a lot Mohsen.
I knew the pattern i just didn't know how to do it in 1 for loop. I was thinking about doing each redundancy bit in their own loop. That helps a lot.
 
Old 02-03-2004, 05:31 PM   #5
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
If your code isn't too long (say 8 bits), it'd probably be easiest to just use a lookup table for finding the redundancy bits; declare an array of size 256, and store the three-bit checksum for code X in array[X]. Might be handy if you're using them repeatedly.

I think the easiest way to calculate them would be to multiply each 8-bit input by an 3x8 matrix that has "1" in the positions that should be added to the result, and "0" in all the rest. So, since bit 1 of the checksum should be bits 1, 3, 5, and 7, the first row of the matrix would be:

Code:
1 0 1 0 1 0 1 0
Bit 2 of the checksum would be row 2 of the matrix, and bit 3 would be row 3. Call it matrix E. Then you get your 8-bit input as an 8x1 column vector M, and multiply it on the right by E:

E x M = C

where C is your 3-bit checksum. You could probably simplify it even more; don't even need to use a matrix, since it's just 1 bit in each position. You could just represent the matrix as three 8-bit variables R1, R2, R3 (one for the 8 bits in each row, so R1 = [10101010], etc.); logical AND M with each of those and XOR them together: (R1 & M) ^ (R2 & M) ^ (R3 & M)

Then again, I've taken a whole university course on this stuff, so if that looks too complicated, then nevermind

Last edited by wapcaplet; 02-03-2004 at 05:51 PM.
 
Old 02-03-2004, 06:58 PM   #6
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
User is allowed enter any number so it can be any size. I am using arraylist (java).
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Question on Code k1ll3r_x Programming 3 11-02-2004 07:41 PM
Can anyone help me to identify what is wrong with this Java code? babyboss Programming 2 10-03-2004 11:28 AM
Why won't this Java code work? ludeKing Programming 9 05-28-2004 11:32 AM
how will i run a exe from java code? ambuj Programming 6 01-24-2004 08:32 AM
what is the best way for student to enter java code Brain Drop Programming 11 01-01-2004 02:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration