Divide a Binary Number

Discussion in 'Math' started by leocircuits, Mar 15, 2016.

  1. leocircuits

    Thread Starter New Member

    Mar 15, 2016
    7
    0
    How can I Divide a Binary Number,
    Exp: 72/12 & 100/5,
    the Answers need to be in Binary...
     
  2. crutschow

    Expert

    Mar 14, 2008
    13,000
    3,229
    To you want to do this by hand, with a digital circuit, or with a microprocessor?
     
  3. StayatHomeElectronics

    Well-Known Member

    Sep 25, 2008
    864
    40
    First you would need to convert them into binary numbers...
     
    ErnieM and Robin Mitchell like this.
  4. leocircuits

    Thread Starter New Member

    Mar 15, 2016
    7
    0
    My son has this problem for Homework, I don't know how to solve it....
     
  5. leocircuits

    Thread Starter New Member

    Mar 15, 2016
    7
    0
    It is Paper Homework, he needs to include steps .
     
  6. crutschow

    Expert

    Mar 14, 2008
    13,000
    3,229
    Perhaps this will help.
     
    leocircuits likes this.
  7. WBahn

    Moderator

    Mar 31, 2012
    17,720
    4,788
    Does your son know how to divide multi-digit decimal numbers by hand, such as

    206901 / 873

    The steps involved are identical to how you work with binary numbers (or any other number base). So if this isn't an issue, then the only issue is converting the decimal numbers to binary in the first place. There are a few ways to do it, but perhaps the easiest conceptually (though by far not the most efficient) is to just use the definition of a positional numbering system.

    If your binary number is written as

    x = hgfedcba

    where each letter is either a 0 or a 1, then the "weight" of the 'a' bit is 2^0 = 1, the weight of the 'b' bit is 2^1 = 2, the weight of the 'c' bit is 2^2 = 4, and so on up through 'h' being 2^7 = 128.

    This is the same as in decimal were the right-most digit has a weight of 10^0 = 1, the next digit 10^1 = 10, the next one 10^2 = 100, and so on.
     
  8. leocircuits

    Thread Starter New Member

    Mar 15, 2016
    7
    0
    Loved the Excel Program..

    Thanks....
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Most any windows PC should have a calculator program. If you hunt thru the menu you will find a scientific calculator, and that can be set to work in binary. You can even change modes to do a conversion, or enter 7 in decimal, change to binary and get 111.

    The problem should be done on paper using old fahioned long division but this can help you check each step.
     
  10. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    761
    Could you show how the homework was stated?
     
  11. ian field

    Distinguished Member

    Oct 27, 2012
    4,413
    782
    Last time I checked; binary division was done by shifting the whole byte/word one (or more) places sideways.
     
  12. ian field

    Distinguished Member

    Oct 27, 2012
    4,413
    782
    Being a bit flash - I have a couple of calculators that work in various number bases.

    They're at least 10 digit display, but you still cant do much in binary.
     
  13. StacyB

    New Member

    May 19, 2016
    1
    0
    The first thing you need to do is convert the number to binary.

    So 72 would be 1001000.

    then to divided by 12 you would first divide the number by 4 and then by 3.

    To divide by 4 you would shift the number 2 bits to the right. So 1001000 becomes 10010(18).

    Then to divide by 3 you would divide by 2 and then subtract binary 3. Dividing by 2 again 10010 becomes 1001(9). Then subtracting 11(3) 1001 becomes 110 or 6.

    To divide 100 by 5 you would divide by 4 by shifting the number 2 bits to the right and then subtract binary 5.

    So dividing by 4 1100100(100) becomes 11001(25) and then subtracting 101(5) 11001 becomes 10100 or 20.
     
  14. MrChips

    Moderator

    Oct 2, 2009
    12,432
    3,360
    Totally wrong.

    Who told you this? Or are you simply doing the shifts and correcting the result by subtracting what is left over?
    Try your algorithm with different numbers and see if it still works.

    BTW - Two months later, I think the OP is not coming back.
     
  15. hp1729

    Well-Known Member

    Nov 23, 2015
    1,948
    219
    Do the math in decimal then convert the answer ti binary,
    Use a scientific calculator that does binary.

    You do it the same way you do decimal. First convert the numbers to binary.
    Then divide one number into the other just like you would if they were decimal.

    Google "binary division".
     
    Last edited: May 19, 2016
  16. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,681
    900
  17. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    As long as this thread left the tracks long ago...

    Once I had one rather short program to fit in a tiny micro controller with but one simple division to perform, something like a 16 bit number divided by an 8 bit, both unsigned. When using a C compiler I found that adding the one division made the program just large enough not to fit in my device. I had to use that device for many other reasons.

    The work around was to do division by subtraction, literally count how many times I could subtract B from A and still get a positive result.

    I had plenty of time to accomplish this task so a simple way worked out just fine.
     
  18. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,674
    2,724
    Not that it matters wrt to this thread, but division doesn't take all that much code. Here's an old 48 bit/24 bit routine I have lying around. It's be smaller for 16/8.

    Code (ASM):
    1.  
    2. ;*************************************
    3. ;** DIV48x24 -- 48bit / 24 bit      **
    4. ;** OPER15:10 = PROD5:0 / OPER02:00 **
    5. ;*************************************
    6.  
    7. div48x24
    8.     movlf   bitcnt,25       ;set 24 bit division
    9.  
    10. clrquo  clrf    oper10
    11.     clrf    oper11
    12.     clrf    oper12
    13.     clrf    oper13
    14.  
    15.     movfw   oper00          ;make sure divisor is not zero (will hang)
    16.     iorwf   oper01,w
    17.     iorwf   oper02,w
    18.     bz  normal          ;do fake div without normalizing
    19.  
    20.     clrc
    21. nordom  bbs oper02,7,0,normal
    22.     rlcf    oper00,f
    23.     rlcf    oper01,f
    24.     rlcf    oper02,f
    25.     incf    bitcnt,f
    26.     bra nordom
    27.    
    28. normal  clrf    divhi
    29.  
    30. divlp   movfw   oper00
    31.     subwf   prod3,w
    32.     movwf   divtmp0
    33.  
    34.     movfw   oper01
    35.     subwfb  prod4,w
    36.     movwf   divtmp1
    37.    
    38.     movfw   oper02
    39.     subwfb  prod5,w
    40.     movwf   divtmp2
    41.    
    42.     clrw
    43.     subwfb  divhi,w
    44.  
    45.     bm  divneg
    46.  
    47.     movwf   divhi
    48.     movff   divtmp2,prod5
    49.     movff   divtmp1,prod4
    50.     movff   divtmp0,prod3
    51.  
    52. divneg  rlcf    oper10,f
    53.     rlcf    oper11,f
    54.     rlcf    oper12,f
    55.     rlcf    oper13,f
    56.     rlcf    oper14,f
    57.     rlcf    oper15,f
    58.  
    59.     rlcf    prod0,f
    60.     rlcf    prod1,f
    61.     rlcf    prod2,f
    62.     rlcf    prod3,f
    63.     rlcf    prod4,f
    64.     rlcf    prod5,f
    65.     rlcf    divhi,f
    66.  
    67.     djnz    bitcnt,divlp
    68.  
    69.     return
    70.  
    71.  
     
  19. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Agreed. Somewhere I pasted a compiler output for some simple division routine that could have worked, but since I fairly work in assembler any more maintaining C code in a C project is far preferable to me, and whomever might have picked up that task now that I have left that company.
     
  20. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    761
    Hola John

    For the pleasure of it, I wrote the code to implement it with the 18F family. It worked at the first try.

    Code (Text):
    1. Assembly  
    2.  
    3. ;DIV 216U KENYAN.ASM
    4.  
    5.  
    6. DIV_3216U_KEN               ;unsigned 32/16-bit values division (KENYAN)
    7.  
    8. ;Agustin T. Ferrari Nicolay - Buenos Aires - April 2007
    9.  
    10. ;Algorithm implemented:
    11.  
    12. ;Given DIVIDEND and DIVISOR, initialize QUOTIENT =0 and INDEX_DSOR =0
    13.  
    14. ;Duplicate DIVISOR repeatedly. Increase PNTR_DSOR every time DIVISOR is doubled
    15.  
    16. ;Repeat doubling to get a DSOR equal to, or the closest below DIVIDEND.
    17.  
    18. ;Substract the highest DSOR from DIVIDEND setting b0 of QUOTIENT.
    19.  
    20. ;Shift QUOTIENT to left and decrement PNTR_DSOR.
    21.  
    22. ;Succesively try to substract from the resulting remainder above, every DIVISOR
    23.  
    24. ;value, down in the list.
    25.  
    26. ;For every possible substraction, keep setting b0 of QUOTIENT and shifting it
    27.  
    28. ;to left. If not possible, just shift QUOTIENT to the left.
    29.  
    30. ;In any case, always decrement PNTR_DSOR.
    31.  
    32. ;Once finished (PNTR_DSOR again =0), the remainder is the result of the last
    33.  
    34. ;substraction. QUOTIENT in the corresponding register.
    35.  
    36.  
    37. ;To call the routine:
    38. ;dividend in DEND_3:0 (max val H'FFFE 0001' = H'FFFF' * H'FFFF' =4.294.836.225)
    39. ;divisor in DSOR_1:0 (max value H'FFFF' =65.535) - DSOR_3:2 used internally in
    40. ;the routine
    41.  
    42. ;User to ensure being within range or if division by zero is attempted.
    43.  
    44. ;The routine gives:
    45. ;Result of DEND_3:DEND_0 / DSOR_1:DSOR_0 => QUOT_H:QUOT_L.
    46. ;Remainder in DEND_3:0
    47.  
    48.     CLRF DSOR_3
    49.     CLRF DSOR_2
    50.     CLRF QUOT_H
    51.     CLRF QUOT_L
    52.     CLRF PNTR_DSOR
    53.  
    54. DIV_3216U_KEN_INC_DSOR_LOOP
    55.     BCF STATUS,C            ;ensure b0 of DSOR_0 is clear after the shifting
    56.     RLCF DSOR_0,F           ;DSOR =DSOR*2
    57.     RLCF DSOR_1,F
    58.     RLCF DSOR_2,F
    59.     RLCF DSOR_3,F
    60.    
    61.     INCF PNTR_DSOR,F
    62.  
    63.     MOVF DSOR_3,W
    64.     SUBWF DEND_3,W
    65.     BNZ CHKIF_DSOR3_GT_DEND3
    66.    
    67.     MOVF DSOR_2,W
    68.     SUBWF DEND_2,W
    69.     BNZ CHKIF_DSOR2_GT_DEND2
    70.    
    71.     MOVF DSOR_1,W
    72.     SUBWF DEND_1,W
    73.     BNZ CHKIF_DSOR1_GT_DEND1
    74.    
    75.     MOVF DSOR_0,W
    76.     SUBWF DEND_0,W
    77.     BC DIV_3216U_KEN_INC_DSOR_LOOP
    78.    
    79. DIV_3216U_KEN_SUBST_DSOR_LOOP
    80.     TSTFSZ PNTR_DSOR
    81.     BRA DIV_3216U_KEN_DECR_PNTR
    82.     RETURN
    83.  
    84. DIV_3216U_KEN_DECR_PNTR
    85.     DECF PNTR_DSOR          ;we look down in the list of double DSOR values
    86.    
    87.     BCF STATUS,C            ;ensure b0 of QUOT_L is clear after shifting
    88.     RLCF QUOT_L,F           ;shift QUOT to the left to have it ready
    89.     RLCF QUOT_H,F           ;for next substraction
    90.  
    91.     BCF STATUS,C            ;ensure b7 of DSOR_3 is clear after shifting
    92.     RRCF DSOR_3,F           ;shift DSOR
    93.     RRCF DSOR_2,F           ;to the right
    94.     RRCF DSOR_1,F           ;to get
    95.     RRCF DSOR_0,F           ;DSOR =DSOR/2
    96.  
    97.     MOVF DSOR_3,W
    98.     SUBWF DEND_3,W
    99.     BNZ CHKIF_DSOR3_LT_DEND3
    100.    
    101.     MOVF DSOR_2,W
    102.     SUBWF DEND_2,W
    103.     BNZ CHKIF_DSOR2_LT_DEND2
    104.    
    105.     MOVF DSOR_1,W
    106.     SUBWF DEND_1,W
    107.     BNZ CHKIF_DSOR3_LT_DEND3
    108.    
    109.     MOVF DSOR_0,W
    110.     SUBWF DEND_0,W
    111.     BNC DIV_3216U_KEN_SUBST_DSOR_LOOP
    112.  
    113. DIV_3216U_KEN_SUBST_DSOR    ;substract DSOR_3:0 from DEND_3:0
    114.     MOVF DSOR_0,W           ;LSB, borrow
    115.     SUBWF DEND_0,F          ;is NOT used
    116.  
    117.     MOVF DSOR_1,W           ;borrow
    118.     SUBWFB DEND_1,F         ;IS used
    119.  
    120.     MOVF DSOR_2,W           ;borrow
    121.     SUBWFB DEND_2,F         ;IS used
    122.  
    123.     MOVF DSOR_3,W           ;borrow
    124.     SUBWFB DEND_3,F         ;IS used
    125.  
    126.     BSF QUOT_L,0            ;flag "a valid substraction from dividend occurred"
    127.     BRA DIV_3216U_KEN_SUBST_DSOR_LOOP
    128.  
    129. CHKIF_DSOR3_GT_DEND3
    130.     BNC DIV_3216U_KEN_SUBST_DSOR_LOOP
    131.     BRA DIV_3216U_KEN_INC_DSOR_LOOP
    132.  
    133. CHKIF_DSOR2_GT_DEND2
    134.     BNC DIV_3216U_KEN_SUBST_DSOR_LOOP
    135.     BRA DIV_3216U_KEN_INC_DSOR_LOOP
    136.  
    137. CHKIF_DSOR1_GT_DEND1
    138.     BNC DIV_3216U_KEN_SUBST_DSOR_LOOP
    139.     BRA DIV_3216U_KEN_INC_DSOR_LOOP
    140.  
    141. CHKIF_DSOR3_LT_DEND3
    142.     BC DIV_3216U_KEN_SUBST_DSOR
    143.     BRA DIV_3216U_KEN_SUBST_DSOR_LOOP
    144.  
    145. CHKIF_DSOR2_LT_DEND2
    146.     BC DIV_3216U_KEN_SUBST_DSOR
    147.     BRA DIV_3216U_KEN_SUBST_DSOR_LOOP
    148.  
    149. CHKIF_DSOR1_LT_DEND1
    150.     BC DIV_3216U_KEN_SUBST_DSOR
    151.     BRA DIV_3216U_KEN_SUBST_DSOR_LOOP
    152.  
     
    jpanhalt likes this.
Loading...