Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Binary div in C/C++

1. ## 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
}```
can someone come up with an implementation?
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...

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;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•