Saturday 6 October 2012

Topic 1 Subtopic 5: Hexadecimal Number Operation


Hexadecimal numbers are a compact way of representing large numbers. They are useful in computer programming because computers use bytes as their main unit of information. A byte can represent an alphanumeric character or one of 256 decimal numbers. It normally requires 8 binary digits to represent a byte but in hexadecimal only two are required. In base ten, we had digits 0 through 9. In base eight, we had digits 0 through 7. In base 4, we had digits 0 through 3. In any base system, you will have digits 0 through one-less-than-your-base. This means that, in hexadecimal, we need to have "digits" 0 through 15. To do this, we would need single solitary digits that stand for the values of "ten", "eleven", "twelve", "thirteen", "fourteen", and "fifteen". But we don't. So, instead, we use letters. That is, counting in hexadecimal, the sixteen "numerals" are:

    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

In other words, A is "ten" in "regular" numbers, B is "eleven", C is "twelve", D is "thirteen", E is "fourteen", and "F" is fifteen.  It is this use of letters for digits that makes hexadecimal numbers look so odd at first. But the conversions work in the usual manner.

Hexadecimal Arithmetic

Hexadecimal Addition

Adding hexadecimal numbers is very much the same as adding decimal numbers. When adding decimals, a number is carried over into the next column when the sum of two numbers is greater than 9. In hexadecimal addition, a number is carried over when the sum of two hexadecimal numbers is greater than 15.

For example:

    $8 + $C = $14

This sum may look a little strange but if we look at the decimal equivalent, 8 + 12 = 20, it makes sense. Twenty is four more than sixteen so we put a 4 down in the ones column and carry a 1 over into the sixteens column.

A more complicated example:

    $2D + $04 = $31


Breaking it down into columns, we are adding 4 to D, which is the equivalent of 4 + 13 = 17 = $11. Seventeen is one more than sixteen so 1 is marked in the ones column and a 1 is carried over into the sixteens column. The one that is carried over is added to the two that is already in the sixteens column giving 3.

Hex Addition example

   $ 3A
+ $ 28
--------
$ 62


A+8 = 2; with carry out of 1 to next column;

1 (carry) + 3 + 2 = 6;
answer = $ 62.



Decimal check

$ 3A = 3 x 16 + 10
        = 58
$28 = 2 x 16 + 8

       = 40
58 + 40 = 98
$ 62 = 6 x 16 + 2
        = 96 + 2 

        = 98 (proved)


Subtraction

Subtraction is no different:

    $A3 - $0D = $96

Hexadecimal Subtraction

Looking at the columns again, $D (13) is being subtracted from $3. There is not enough in the ones column so we must borrow one from the sixteens column. This reduces the sixteen column to $9 and leaves $13 in the ones column for $D to be subtracted from.

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:

Hex Subtraction

   $ 34
- $ 27
--------
  $ 0D
4-7 = D; with borrow of 1 from next column;
3 - 1 (borrow) - 2 = 0.
answer = $ 0D.




Decimal check

$ 34 = 3 x 16 + 4
        = 52
$27 = 2 x 16 + 7
        = 39
52 - 39 = 13
$ 0D = 13(proved)


Hexadecimal Multiplication

 

To make the conversion by assigning each place in the source base the hexadecimal representation of its place value and then performing multiplication and addition to get the final representation. 
That is, to convert the number CA89 to decimal one can split the conversion into 
C (12), A (10), 3 (8) and 9 (9)  [the number in ( ) is the value of hexadecimal in base 10],
then get the final result by multiplying each decimal representation by 16^p, where 'p' is the corresponding position from right to left, beginning with 0. 
In this case we have :
(12 × 16^0) + (10 × 16^1) + (8 × 16^2) + (9 × 16^3), 
which is equal to 51849 in base 10.


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 we'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.

Hexadecimal Multiplication Table


* 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




Liew Jun Jie               B031210374

3 comments:

  1. It will be better if it were more understandable as if it were more interesting

    ReplyDelete
  2. good reference.. many examples will be helpful

    ReplyDelete
  3. Easy to understand. Good work.

    ReplyDelete