Hexadecimal Numbers

Home

Internal Representation of Numbers: Hexadecimal

We have looked at the decimal and binary number systems.  Now we are ready for another, base 16 or hexadecimal (hex for short).

The hexadecimal number system is commonly used to represent computer data, because it allows for a shorthand method of representing binary data.

Since a byte is commonly the smallest unit of memory addressed, and one byte consists of 8 bits, we often express binary data by symbols representing groupings of four bits (Binary Digits). (Therefore, one byte can be represented by two hexadecimal symbols).

The hexadecimal number system allows one to express the 16 possible distinct values of those four bits with a corresponding symbol.

The end result is that, instead of printing 8 symbols, representing the eight binary digits in a byte, we can represent the contents of a byte using only two symbols. This allows for more compact representation of internal data.

As well, if we need to determine the binary data represented by a hexadecimal number, we only have to convert each of the two hexadecimal symbols back to their binary equivalent.

Lets do our 137 decimal conversion again, only this time in Hexadecimal.

Remember that 137 10  represents 1 unit of 10 raised to the second power, 3 units of 10 raised to the first power, and 7 units of 10 raised to the zeroth power.

    1x102  + 3x101  + 7x100   = 137   , or

    1x100 + 3x10  + 7x1    = 137

Hexadecimal Number System

bulletThe binary system consists of 2 symbols (0 & 1).
bulletThe decimal system consists of 10 symbols (0,1,2,...,9).
bulletSince we require 16 symbols for the hexadecimal number system, we can use the 10 we're already used to, and we "borrow" 6 more symbols from the alphabet.  Together, they represent each of the 16 possible distinct values of a group of 4 bits.
The Hexadecimal Number Symbol Values
Binary number Decimal Equivalent Hexadecimal Symbol
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F

Hex to Decimal Conversion

How would we convert 137 to hex? Start with a lookup table:

16**0 = 1
16**1 = 16
16**2 = 256
16**3 = 4096
16**4 = 65536

bulletLook up the powers of 16 until we get one that is larger than the current number.
bulletIn this case 16**2 is 256 and greater than 137.
bulletThis means we have at least one unit of 16**1.
bulletDivide 137 by 16 and we get 8 and a remainder of 9.
bullet8  represents the number of groups of 16**1.
bulletSince there are no powers of 16 less than our remainder of 9, we have 9 units of 16**0 and we are done.
bulletTherefore, 137 base 10  =  89 base 16       ( 137 10 = 89 16 )
This is the equivalent of writing 137 10 = 8 * 16 1 + 9 * 16 0 = 8 * 16 + 9 * 1 = 128 + 9 = 137.

Convert the following from decimal to hex:

  1. 1345
  2. 12000
  3. 78
  4. 455
Convert the following from hex to decimal:
  1. 1345
  2. 1A2B
  3. D0A
  4. BED

Conversion between Binary and Hexadecimal

As mentioned, the main reason for using the hexadecimal number system is as a shorthand method to represent binary data. Therefore, the conversion between binary and hexadecimal numbers is relatively straightforward.

Binary to Hexadecimal

bulletArrange the binary number into groups of 4 bits starting from the right (the least significant digit) in the binary number.
bulletConvert the resulting groups to their hex equivalent.

Therefore,  10101100000011 2 can be converted by

bulletgrouping binary digits from the right in groups of four               10 | 1011 | 0000 | 0011
bulletConverting each group to its hexadecimal equivalent                  2        B         0        3
bullet10101100000011 = 2B03 16

Hexadecimal to Binary

bulletconvert hexadecimal symbols to their binary equivalent (ensuring that you remember each hexadecimal digit represents four binary digits ).
bullet9A04 16  = 1001 1010 0000 0100 2
At this point you should be able to do conversions of positive numbers to any base.
  1. 756 decimal to hex.
  2. 513A hex to binary.
  3. 67B hex to decimal.
  4. 115 decimal to hex.
 

Binary Addition and Negative Numbers

At this point we have covered how the computer represents POSITIVE integers. Now we'll look at Negative numbers. This is harder, mainly because of the representation of zero.

Signed Magnitude

Signed magnitude uses the most significant bit in the representation of a number and uses that to be the sign of the number. For example: 137 decimal is represented 0000 0000 1000 1001 in a 16 bit word. A signed magnitude system would represent -137 as 1000 0000 1000 1001. This system has the problem that there are 2 representations for zero: +0 and -0. Ambiguity is a bad thing with computers. Also, (a - b) and (a + (-b)) are not the same. A computer that uses signed magnitude representation of integers must add extra circuitry to deal with these problems. 

One's Complement

One's complement is just the positive number with all the bits reversed.

(137)10 = 0000 0000 1000 1001 would be 1111 1111 0111 0110.

This was used in the Univac 1100 series (1962) but has a similar problem to signed magnitude: that there is two representations for zero: 0000 0000 0000 0000 and 1111 1111 1111 1111. Both are valid bit patterns.

Two's Complement

A two's complement is formed by adding one to a One's Complement. This has the properties of every bit pattern is a valid number and every number is unique. This system is used by most, if not all, computers today. So, let's look at binary math with Two's Complement numbers. 

Look at the examples.

Binary Addition

Identical in method to decimal you just have to remember there is only 2 digits -  0 and 1.

0 + 1 = 1 and 1 + 1 = 10  (0 and carry the 1 into the next position)

       0       01     10    11
      +1       +1     +1    +1
    _____    ____   ____  ____
       1       10     11   100

To convert a positive number to a negative number in two's complement do a one's complement and add 1.

For example:                        0000 0000 0000 0001
as a 1's complement is              1111 1111 1111 1110
add 1 to become a 2's complement is 1111 1111 1111 1111.

Examples:
 
Decimal value  Binary Value One's complement (Negative) Two's complement (Negative)

137  

0000 0000 1000 1001 1111 1111 0111 0110 1111 1111 0111 0111

345  

0000 0001 0101 1001 1111 1110 1010 0110 1111 1110 1010 0111

1434  

0000 0101 1001 1010 1111 1010 0110 0101 1111 1010 0110 0110

Give the internal representation of the following numbers.

bullet-13 in signed magnitude
bullet-1315 in one's complement
bullet-4986 in two's complement
bullet-3130 in two's complement
bullet-35986 in two's complement

Binary Addition Using Two's Complement

One of the nice features of using two's complement representation is the ability to perform mathematical operation involving integers in terms of binary additions only.

109 - 59 can be represented as 109 + (-59).

When we have the computer evaluate this expression,  internally the CPU is actually performing a binary addition by first converting the negative operand to its Two's Complement representation and then performing an addition.

Continuing with our example,
 

  1. 109 10 is               0000 0000 0110 1101 2
  2.  59 10 is               0000 0000 0011 1011 2

    reverse bits.     1111 1111 1100 0100
    add 1.            1111 1111 1100 0101 

  3. Now perform binary addition.

      0000 0000 0110 1101 
      1111 1111 1100 0101 + 
      -------------------

    1 0000 0000 0011 0010

    The most significant bit goes beyond the boundary of the byte and is ignored.


    Therefore the result is:

    0000 0000 0011 0010, or

    1*32+1*16+1*2

    = 50          (109 - 59)
     

You try 59 - 109 ?
 

Last updated: September 11, 2003.