Search code examples
c++pseudocode

Dividing n-bit binary integers


Was wondering if anyone could help me with creating a pseudocode for how to go about dividing n-bit binary integers. Here is what I'm thinking could possibly work right now, could someone correct this if I'm wrong:

divide (x,y)
     if x=0: return (0,0) //(quotient, remainder)
     (q,r) = divide(floor(x/2), y)

     q=2q, r=2r
     if x is odd: r = r+1

     if r >= y: r = r-y, q = q+1
          return (q,r)

Would you guys say that this general pseudocode algorithm would accomplish the intended task of dividing n-bit numbers or am I missing something in my psuedocode before I start coding up something that's wrong?


Solution

  • Other then the obvious stuff (not checking for division by zero, not handling negative numbers), it seems to be working. I convinced myself by just applying this to base-10 numbers.