Binary Division

For class I need to write a program that inputs a file (the dividend), performs binary division on the file (using 0x12 as the divisor), and outputs the remainder(checksum).

I have researched binary division algorithms and I get the general gist, but I'm still unsure where to start. How would I store the dividend and divisor? As two arrays of bits?

Then, I need to figure out how to perform shifts and XORs on the the binary numbers.
Maybe I should use bitwise operations?

Any suggestions would be greatly appreciated.
Last edited on
I don't think you need to work with bits for this. 0x12 is equivalent to the decimal 18, so x/0x12 is the same as x/18.

All of the fundamental data types are stored as bytes and bytes are made of bits (or words), so one standard of an array of bits is a word (four bits, I think). Or, you can use char; I believe the C++ standard guarantees that char will always be 8 bits.
Last edited on
Thanks for the reply.

Actually, the way I understand it, we are using polynomial binary division--which is different. So, the binary numbers can not be converted to decimal.
Last edited on
Okay, I see.

Well, for more than 8 bits, you can store the coefficients of the dividend and divisor in std::vector<bool> or std::bitset<N>.
In your case, std::bitset might work better because you can use bitwise operations on the entire container, e.g.
   b3 = b1 & b2,
   b4 = b1 | b2,
   b5 = b1 ^ b2,
   b6 = b1 >> 3,
   b7 = b2 << 4
Last edited on
Thanks for the suggestion. This certainly seems worth looking into.

Please if anyone else has any suggestions let me know.

wanna just make a binary clock? ;D

There are only 10 kinds of people: those who understand binary, and those who don't.
Last edited on
Topic archived. No new replies allowed.