Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 2 of 2
Thread: Binary div in C/C++

03072007, 02:58 AM #1
 Join Date
 Jan 2007
 Posts
 12
 Thanks
 1
 Thanked 0 Times in 0 Posts
Binary div in C/C++
Hey there,
I'm having a hard time coming out with a binary div function. I'm using this pseudocode as my guide:
Code:function crc(bit array bitString[1..len+n], int len) { remainderPolynomial := polynomialForm(bitString[1..n]) // First n bits of the message // A popular variant complements remainderPolynomial here for i from 1 to len { remainderPolynomial := remainderPolynomial * x + bitString[i+n] * x0 if coefficient of xn of remainderPolynomial = 1 { remainderPolynomial := remainderPolynomial xor generatorPolynomial } } // A popular variant complements remainderPolynomial here return remainderPolynomial }
This is for a cyclicRedundancyCheck (all in all, all i'd like is 2 binary nums divided).
I'm aware that binary division are done by shifting and xoring, can anyone come up with a better idea?
EDITED: Well I decided that was too complicated so I did it in a different manner...Last edited by klam; 03072007 at 08:54 PM. Reason: Solved.

03072007, 09:04 PM #2
Is this cheating?
Code:#include <iostream> #include <bitset> using namespace std; class DivisionByZeroError {}; template <size_t bit_len> inline bitset<bit_len> operator/(const bitset<bit_len>& numerator, const bitset<bit_len>& denominator) { if (denominator.none()) { throw DivisionByZeroError(); } return numerator.to_ulong()/denominator.to_ulong(); } int main(int, char**) { static const unsigned MAX_BIT_LEN = 16; bitset<MAX_BIT_LEN> b1 = 32; bitset<MAX_BIT_LEN> b2 = 4; cout << b1 << "/" << b2 << " = " << b1 / b2; return 0; }
Last edited by ralph l mayo; 03072007 at 09:52 PM. Reason: More complete example with syntactic sugar. Same issuedodging core.