Hexadecimal Subtraction and Multiplication

Date: 04/06/2000 at 11:53:40
From: Peggy Schnurr
Subject: Hexadecimal multiplication

I understand adding hexadecimal numbers, but I'm having trouble 
understanding multiplication and subtraction of hexadecimal numbers. I 
would appreciate any direction you can offer on the subject.

Thank you.
Peggy Schnurr

Date: 04/06/2000 at 17:21:07
From: Doctor TWE
Subject: Re: Hexadecimal multiplication

Hi Peggy - thanks for writing to Dr. Math.

Let's tackle subtraction first. Subtraction in hexadecimal works 
similar to subtraction in decimal. Let's do a pair of decimal problems 
and see if we can "parallel" the solution method in hex.

Suppose we want to subtract 1586 - 243 (in decimal). We write the 
subtrahend (the number to be subtracted) underneath the minuend (the 
number to subtract from), lining up the place values at the decimal 
point - or at the right if there is no decimal point - like this:

       1586
     -  243
       ----

Now, starting from the right, we subtract the digits. (Ones place) 
6-3 = 3, (tens place) 8-4 = 4, (hundreds place) 5-2 = 3, (thousands 
place) 1-0 = 1. It looks like this:

       1586
     -  243
       ----
       1343

We do the same thing in hexadecimal, except that we occasionally have 
a digit larger than 9. For example, suppose we want to subtract 
5CD2 - 2A0 (hexadecimal). As with decimal, we write the subtrahend 
(the number to be subtracted) underneath the minuend (the number to 
subtract from), lining up the place values at the radix point - or at 
the right if there is no radix point - like this:

       5CD2 h   (The 'h' just indicates a hexadecimal value)
     -  2A0 h
       ------

Now, starting from the right, we subtract the digits. (Ones place) 
2-0 = 2, (sixteens place) D-A = 13-10 = 3, (256's place) 
C-2 = 12-2 = 10 = A, (4096's place) 5-0 = 5. It looks like this:

       5CD2 h
     -  2A0 h
       ------
       5A32 h

That's fine - until you get a place value in which there's not enough 
to subtract from. What do we do in decimal? We borrow from the next 
higher place value. For example, suppose we want to subtract 
3145 - 1976 (decimal). We'll align our numbers:

       3  1  4  5
     - 1  9  7  6
       ----------

Now in the ones place, we can't subtract 6 from 5 (5 is less than 6) 
so we borrow 1 from the tens place. Since that has a place value (a 
"weight") of ten, we can exchange it for 10 ones. (This is kind of 
like changing a $10 bill into ten $1 bills.) This leaves us with 3 
tens (4 minus the 1 we borrowed) and gives us 15 ones (5 plus the 10 
we got from the borrow). We now subtract 15-6 = 9:

             3 15
       3  1  \  \   (There was a 4 and a 5 under the '\'s)
     - 1  9  7  6
       ----------
                9

Similarly in the tens place, we can't subtract 7 from 3 so we borrow 
one from the hundreds place. Since that has a place value (a "weight") 
of one hundred, we can exchange it for 10 tens. (This is kind of like 
changing a $100 bill into ten $10 bills.) This leaves us with no 
hundreds (1 minus the 1 we borrowed) and gives us 13 ones (the 3 left 
over after the previous borrow plus the 10 we got from this borrow). 
We now subtract 13-7 = 6:

          0 13 15
       3  \  \  \
     - 1  9  7  6
       ----------
             6  9

We do the same for the hundreds, borrowing 1 from the thousands, then 
subtract the thousands. We end up like this:

       2 10 13 15
       \  \  \  \
     - 1  9  7  6
       ----------
       1  1  6  9

Why did we get 10 from each borrow? Because each time, the next place 
value was 10 times as large. This is because decimal is base-10.

Now let's look at a hexadecimal problem that requires borrowing. For 
example, suppose we want to subtract A8D2 - 3EAC (hexadecimal). We'll 
align our numbers:

       A  8  D  2  h
     - 3  E  A  C  h
       -------------

Now in the ones place, we can't subtract C (12) from 2 so we borrow 1 
from the sixteens place. Since that has a place value (a "weight") of 
sixteen, we can exchange it for 16 ones. (This is kind of like 
changing a $16 bill into sixteen $1 bills.) This leaves us with 12 
sixteens (D = 13 minus the 1 we borrowed) and gives us 18 ones (2 plus 
the 16 we got from the borrow). We now subtract 18-12 = 6:

                       (Note that I use decimal here. Some people 
            12 18       write these as Ch and 12h.)
       A  8  \  \  h   (There was a D and a 2 under the '\'s)
     - 3  E  A  C  h
       -------------
                6  h

In the sixteens place, we don't need to borrow because we can subtract 
10 (A) from 12:

            12 18
       A  8  \  \  h
     - 3  E  A  C  h
       -------------
             2  6  h

In the 256's place, we again need to borrow. We'll borrow 1 from the 
4096's place and exchange it for sixteen 256's (one 4096 equals 
sixteen 256's). This leaves us 9 in the 4096's place (A = 10 minus the 
1 that we borrowed), and gives us 24 in the 256's place (8 plus the 16 
from the borrow). We then can subtract 24-14 = 10 = A. So we have:

       9 24 12 18
       \  \  \  \  h
     - 3  E  A  C  h
       -------------
          A  2  6  h

Finally, we subtract 9-3 = 6 in the 4096's place:

       9 24 12 18
       \  \  \  \  h
     - 3  E  A  C  h
       -------------
       6  A  2  6  h

Why did we get 16 from each borrow? Because each time, the next place 
value was 16 times as large. This is because hexadecimal is base-16.

One final note: If the subtrahend (the bottom number) is larger than 
the minuend (the top number), flip the numbers around and make the 
final answer negative, just as you would in decimal.

I'll send you information on multiplication tomorrow, as I have run 
out of time today. (I have a 5-year old I must pick up from 
kindergarten.)

I hope this helps. If you have any more questions, write back.

- Doctor TWE, The Math Forum
  http://mathforum.org/dr.math/   

Date: 04/06/2000 at 13:04:55
From: Doctor Peterson
Subject: Re: Hexadecimal multiplication

Hi, Peggy.

All the operations work exactly the same in any base, as long as you 
use the right tables and remember to use the base when you carry or 
borrow.

For example, here's 25 x 3A (37 x 58 base 10):

       25
     x 3A
     ----
       32   A x 5   (10 x 5 = 50)
      14    A x 2   (10 x 2 = 20)
       F    3 x 5   (3 x 5 = 15)
      6     3 x 2   (3 x 2 = 6)
     ----
      862 = 2146 (base 10)

I wrote out each product of a pair of digits here, to save carrying, 
and to show explicitly the four numbers you would get from a 
multiplication table (though I just multiplied each pair in base 10 
and converted to base 16); normally you'd write it as

       25
     x 3A
     ----
      172   = 32 + 140
      6F    = F + 60
     ----
      862

Here, for example, 5 x A = 32, so I put down 2 and carried the 3, 
which I added to the result of A x 2 = 14 to get 17. When I added the 
partial products, I found that 7+F = 16, so I put down the 6 and 
carried the 1.

If you haven't looked at our FAQ on number bases, please do so:

   http://mathforum.org/dr.math/faq/faq.bases.html   

If there's something you still have trouble with, show me your work on 
a sample problem so I can see what part to explain.

- Doctor Peterson, The Math Forum
  http://mathforum.org/dr.math/   

Date: 04/07/2000 at 11:05:47
From: Doctor TWE
Subject: Re: hexadecimal multiplication

Hi again, Peggy.

I see Dr. Peterson has already answered you regarding multiplication. 
But since I had already constructed most of this response yesterday, I 
thought I'd finish it and send it anyway.

Let's look at a decimal multiplication problem; for example 325 * 18. 
Again, we align the numbers by the decimal point:

         365
     *    28
       -----

We start with the ones digit of the multiplier (the bottom number). We 
multiply this by the ones digit of the multiplicand (the top number), 
8*5 = 40. We record the 0 in the ones digit of the first partial 
product line, and keep the 4 as a carry:

          4
         365
     *    28
       -----
           0

Next, we multiply the ones digit of the multiplier by the tens digit 
of the multiplicand and add the carry; 8*6+4 = 48+4 = 52. We record 
the 2 in the tens digit of the first partial product line, and keep 
the 5 as a carry:

         54
         365
     *    28
       -----
          20

Now we multiply the ones digit of the multiplier by the hundreds digit 
of the multiplicand and add the carry; 8*3+5 = 24+5 = 29. We record 
the 2 in the tens digit of the first partial product line. Since this 
is the last digit of the multiplicand, we can bring down the carry and 
record a 29:

        254
         365
     *    28
       -----
        2920

Now we repeat the process with the tens digit of the multiplier, and 
start our answer in the tens digit in the second partial product line. 
I'll do this all at one (I hope you see how I got all this - I also 
"erased" my earlier carries):

         11
         365
     *    28
       -----
        2920
        720

Finally, we add the partial products:

         365
     *    28
       -----
        2920
        730
       -----
       10220

How did we know that 8*5 = 40, 2*6 = 12, etc.? We probably memorized 
these as a multiplication table in elementary school. We'll need such 
a multiplication table for hexadecimal. Here it is:

     X | 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
     --+-----------------------------------------------
     0 | 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
     1 | 0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
     2 | 0  2  4  6  8  A  C  E 10 12 14 16 18 1A 1C 1E
     3 | 0  3  6  9  C  F 12 15 18 1B 1E 21 24 27 2A 2D
     4 | 0  4  8  C 10 14 18 1C 20 24 28 2C 30 34 38 3C
     5 | 0  5  A  F 14 19 1E 23 28 2D 32 37 3C 41 46 4B
     6 | 0  6  C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A
     7 | 0  7  E 15 1C 23 2A 31 38 3F 46 4D 54 5B 62 69
     8 | 0  8 10 18 20 28 30 38 40 48 50 58 60 68 70 78
     9 | 0  9 12 1B 24 2D 36 3F 48 51 5A 63 6C 75 7E 87
     A | 0  A 14 1E 28 32 3C 46 50 5A 64 6E 78 82 8C 96
     B | 0  B 16 21 2C 37 42 4D 58 63 6E 79 84 8F 9A A5
     C | 0  C 18 24 30 3C 48 54 60 6C 78 84 90 9C A8 B4
     D | 0  D 1A 27 34 41 4E 5B 68 75 82 8F 9C A9 B6 C3
     E | 0  E 1C 2A 38 46 54 62 70 7E 8C 9A A8 B6 C4 D2
     F | 0  F 1E 2D 3C 4B 5A 69 78 87 96 A5 B4 C3 D2 E1

We do hexadecimal multiplication the same way we did the decimal, only 
we use the hex multiplication table above and hex addition. For 
example, 82E * B4. First, we align the numbers by the radix point:

         82E
     *    B4
       -----

We start with the ones digit of the multiplier and multiply this by 
the ones digit of the multiplicand, 4*E = 38. We record the 8 in the 
ones digit of the first partial product line, and keep the 3 as a 
carry:

          3
         82E
     *    B4
       -----
           8

Next, we multiply the ones digit of the multiplier by the sixteens 
digit of the multiplicand and add the carry; 2*4+3 = 8+3 = B. We 
record the B in the tens digit of the first partial product line 
(there's no carry this time):

          3
         82E
     *    B4
       -----
          B8

Now we multiply the ones digit of the multiplier by the 256's digit of 
the multiplicand (we'd add the carry if we had one); 4*8 = 20. We 
record the 0 in the sixteens digit of the first partial product line. 
Since this is the last digit of the multiplicand, we can bring down 
the carry and record a 20:

        2 4
         82E
     *    B4
       -----
        20B8

Now we repeat the process with the sixteens digit of the multiplier, 
and start our answer in the sixteens digit in the second partial 
product line. I'll do this all in one step (if you don't see how I got 
all this, write back):

        519
         82E
     *    B4
       -----
        20B8
       59FA

Finally, we add the partial products (in hexadecimal):

         82E
     *    B4
       -----
        20B8
       59FA
       -----
       5C058

I hope this helps. If you have any more questions, write back.


'마이크로프로세서' 카테고리의 다른 글

501 ARM Mode와 PSR..너희들은 누구냐?  (0) 2014.04.08
lab4_data processing instruction  (0) 2014.04.01
carry & overflow  (0) 2014.04.01
assembly format  (0) 2014.03.18
하버드 vs 폰노이만  (0) 2014.03.18
Posted by bogus919
,