Thread: modulo 2 division question help

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    5

    modulo 2 division question help

    Would anyone know how to write a program that does binary division (modulo 2) for example, take the number 10110010 which is a binary number, and divides it by 1011 which is also a binary number, then see the remainder.

    this is what i have so far, it works fine for normail integer division e.g 23 / 7

    but it wont work for binary modulo 2 division


    thanks in advance

    Code:
    int posndiv(int divdend, int divisor)
    {
    int quotient;
    int bit_position =1;
    
    if (dividend <=0) {
    quotient = -1; /* error cond.: return -1 */
    } else if(divisor <= 0) {
    quotient = -1; /* error cond.: return -1*/
    } else {
    quotient = 0;
    
    while ((dividend > divisor) && !(divisor & 0x80000000)){
    divisor = divisor << 1;
    bit_position = bit_position << 1;
    }
    
    while (bit_position > 0) {
    if (dividend >= divisor) {
    dividend = dividend - divisor;
    quotient = quotient + bit_posiition;
    }
    divisor = divisor >> 1;
    bit_position = bit_position >> 1;
    }
    }
    return quotient; // quotient = dividend/divisor
    }

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: modulo 2 division question help

    Kudo's for using code tags on your first post!! Unfortunately to get the most out of code tags, you must format your code so it's readable. Try indenting your blocks with spaces.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    >Would anyone know how to write a program that does binary division
    this %2 doesn't work?!
    please forgive me if i haven't understood your question correctly.
    Saravanan.T.S.
    Beginner.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    5
    basically what i want my program to do, is binary division

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by shaq8u
    basically what i want my program to do, is binary division
    What exactly is binary division? Give me an example of your expected input and output.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    5
    what it does is: given two binary numbers in ASCII form from std input, it returns the remainder after mod 2 division (xor).
    this will then be used to implement crc error detecting method which ive already implemented.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    5
    this is an example of binary division

    http://courses.cs.vt.edu/~csonline/...onTextOnly.html

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >http://courses.cs.vt.edu/~csonline/...onTextOnly.html
    404

    Have you seen this?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    5
    its a great help thank you...it would be great if anyones ever written a code like this before, so maybe they could show me.

    Ive seen code on the net, but its just way too big, and too hard to understand, im not an expert at C by any means. but thanks for the help guys

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    //divide by two
    answer = num >> 1;
    //divide by 4
    answer = num >> 2;
    //mod 2
    answer = num & 0x01;
    //mod 4
    answer = num & 0x03;

    is that what you mean?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. Modular Division problem
    By Malek in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2003, 06:08 PM
  4. C++ Simple division question - i hope
    By learning110 in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2003, 06:37 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM