tags:

views:

331

answers:

4

I am parsing binary files and have to implement a CRC algorithm to ensure the file is not corrupted. Problem is that I can't seem to get the binary math working when using larger numbers.

The example I'm trying to get working:

BigInteger G = new BigInteger("11001", 2);
BigInteger M = new BigInteger("1110010000", 2);
BigInteger R = M.remainder(G);

I am expecting:
R = "0101"

But I am getting:
R = "1100"

I am assuming the remainder of 0101 is correct since it is given to me in this book I am using as a reference for the CRC algorithm (it's not based in Java), but I can't seem to get it working. I can get small binary calculations to work that I have solved by hand, but not the larger ones. I'll admit that I haven't worked the larger ones by hand yet, that is my next step, but I wanted to see if someone could point out a glaring flaw I have in my code.

Can anyone confirm or deny that my methodology is correct?

Thanks

+7  A: 

Do the math out yourself. Your numbers are

G=25
M=912
R = 912 % 25 = 12
R = 1100 (binary)

Looks like Java is, in fact, getting you the correct answer. Do it out by hand! Something else is wrong...

Steven Schlansker
Wow.....Don't I feel sheepish.I've been working on implementing this program for so long that I think my brain has slightly melted. I was going to do the math in binary for some reason...Anyway, thanks for the suggestion.
cadwag
+2  A: 

Well, 1100 = 12. 11001 = 25, 1110010000 = 912. 912 % 25 = 12. So all fair. Your book is wrong.

Bozho
A: 

CRC works on polynomials, not numbers, so you need to adapt the math.

starblue
A: 

Your book is correct. The answer is 0101. The reason for the discrepancy is that CRC binary division uses modular addition so that you perform XOR operation on each bit place when you subtract or add so that 11100-11001=00101, not 00011. You can refer to Data Communications and Networking by Forouzan Chp 10.

nahkaimurrao