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 rr lo (?1001010) C=0 no carry rr hi & ; lo (0000000 0?100101) C=0 (answer so far is 0) no carry rr hi & ; lo (0000000 00?10010) C=1 (answer so far is 0) carry! hi+=W (00010010) rr hi & ; lo (00001001 000?1001) C=0 (answer so far is 18*4 or 72) no carry rr hi & ; lo (00000100 1000?100) C=1 carry! hi+=W (00010110) rr hi & ; lo (00001011 01000?10) C=0 (answer so far is 18*4+18*16 or 360) no carry rr hi & ; lo (00000101 101000?1) C=0 no carry rr hi & ; lo (00000010 1101000?) C=1 carry! hi+=W (00010100) rr 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
file: /Techref/SCENIX/lib/math/mul/tutorial_sx.htm, 2KB, , updated: 2004/6/10 13:40, local time: 2024/11/24 02:56,
18.224.52.54:LOG IN
|
©2024 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? <A HREF="http://sxlist.com/TECHREF/SCENIX/lib/math/mul/tutorial_sx.htm"> SX Microcontroller Math Method Tutorial</A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Welcome to sxlist.com! |
.