Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    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
     }
    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...
    Last edited by klam; 03-07-2007 at 09:54 PM. Reason: Solved.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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; 03-07-2007 at 10:52 PM. Reason: More complete example with syntactic sugar. Same issue-dodging core.


  •  

    Posting Permissions

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