please dont rip this site

PIC Microcontroller Basic Math Multiplication Tutorial

Multiplying works just like long multiplication you do on pencil and paper,
but you just use binary. Makes it pretty easy.

Take a look,
         10010100    or   148
       x 00010010       x  18
      -----------       -----
         00000000        1184
        10010100       + 148
       00000000
      00000000
     10010100
    00000000
   00000000
+ 00000000
=================        ====
  000101001101000    or  2664

The way I actually implemented it was like this:
var.lo=multiplicand (i.e. 148)
var.hi=0
W=multiplier (i.e. 18)
I right shift the LSB out of var.
Then 8 times, I do this:
If the bit is set, I add W to the high part.
Right shift the high part (which will shift in overflow from the add)
Right shift the low part. The low part will, as I pull bits out from the
right, will get new bits in from the left from the high part, so IOW, the
multiplicand is being replaced, bit by bit, with the low byte of the answer.

Here's what would happen in the 148 x 14 case:
W=18 (00010010)
lo=148 (10010100)
hi=0
rrf lo (?1001010) C=0

no carry
rrf hi & lo (0000000 0?100101) C=0
(answer so far is 0)

no carry
rrf hi & lo (0000000 00?10010) C=1
(answer so far is 0)

carry! hi+=W (00010010)
rrf hi & lo (00001001 000?1001) C=0
(answer so far is 18*4 or 72)

no carry
rrf hi & lo (00000100 1000?100) C=1

carry! hi+=W (00010110)
rrf hi & lo (00001011 01000?10) C=0
(answer so far is 18*4+18*16 or 360)

no carry
rrf hi & lo (00000101 101000?1) C=0

no carry
rrf hi & lo (00000010 1101000?) C=1

carry! hi+=W (00010100)
rrf hi & lo (00000001 01101000) C=?
(answer so far is 18*4+18*16+18*128 or 2664)

See how easy that was?

How about division... A little more complicated, but the same basic idea.
Pretty much binary long division.

Think of show 2664 / 18 would work in real life:
   ___148__
18 ) 2664
    -18
     --
      86
     -72
      --
      144
     -144
      ---
        0

Now in binary:
      ______10010100__
10010 ) 101001101000
       -10010
        -----
           10110
          -10010
           -----
             10010
            -10010
             -----
                 0

Interested:

Comments:

See also:


file: /Techref/microchip/math/mul/tutorial.htm, 7KB, , updated: 2008/1/18 18:33, local time: 2020/9/19 14:07, owner: pAQ-QSYS-KA9,
TOP NEW HELP FIND: 
75.101.220.230:LOG IN

 ©2020 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/techref/microchip/math/mul/tutorial.htm"> PIC Microcontroller Multiplication Tutorial (tutorial.htm)</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .