Divide Without Operators

Discussion in 'C' started by arpan, Aug 30, 2006.

  1. arpan

    arpan New Member

    Joined:
    Aug 30, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I am a 1st year Electronics & Comm Engineering Student
    i've been programming in c for 2 years....

    I have been told by my senior to

    write a c code for Dividing two integers

    but

    without using any operators ie (+,-,*,/)

    get the Quotient and Remainder

    One can use math.h .

    I cannot figure out this :confused:
    plz help me
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Use the left shift << or Right shift >> operators to achieve the operation.

    Shift left is equivalent to a multiplication by 2.
    Similarly a shift right is equal to division by 2
     
  3. arpan

    arpan New Member

    Joined:
    Aug 30, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Can you give a small code for using left shift << or Right shift >> operators for dividing with any integer
     
  4. kingo

    kingo New Member

    Joined:
    Aug 30, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    suppose if i give my declarations like
    it a=34,b=2,c;

    c=a<<b;


    wht wil be the value of c?????
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Binary of 34 is 100010
    You are asking it for 2 left shift
    So the final no would become
    10001000 which is 136.

    So C will have 136.
     
  6. kingo

    kingo New Member

    Joined:
    Aug 30, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Actually i got the logic in a crude manner if b=1 it is 34*2
    b=2 it is 34*4
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Yes thats true because when you shift bit by one place actually its equivalent to multiplication by 2.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice