2) Number Systems: Dr. E. Lang
2) Number Systems: Dr. E. Lang
1
Table of Contents
2.1 Number Systems
2.2 Binary Number System
2.3 Hexadecimal Number System
2.4 Number System Conversions
2.4.1 Binary to Decimal
2.4.2 Hex to Decimal
2.4.3 Decimal to Binary or Hex
2.4.4 Conversion between Binary and Hex
2.5 Using a Calculator
2.6 Bytes
2.7 Summary
2
2.1) Number Systems
The number system we commonly use is a positional number system
where the value of each digit depends on its position. In addition, it is a
decimal, or base-10, system, since each digit is a multiple of a power of
10. As a consequence, there are only 10 distinct digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Example:
120.56 = 1 x 102 + 2 x 101 + 0 x 100 + 5 x 10-1 + 6 x 10-2
3
2.1) Number Systems
Historically, the first positional number system was the Babylonian
sexagesimal or base-60 system. In this system there will be 59 different
symbols since there is no symbol for 0:
M = 1000
C = 100
D = 50
X = 10
V=5
I=1
So, for example:
1) Binary (Base-2)
2) Hexadecimal, or Hex (Base-16)
6
2.2) Binary Number System
Since a computer is effectively a set of on/off switches, it is
convenient to represent data using a base-2, binary positional number
system. In this case each position represents a power of 2 and there are
only two distinct digits:
0, 1
Note: there is no digit 2 in the binary system, just as there is no digit for
10 in the decimal system.
For computer work, each binary digit is called a bit, and a byte = 8
binary digits or 8 bits.
7
2.2) Binary Number System
Example:
1101 = 1 x 10100 + 1 x 1010 + 0 x 101 + 1 x 100
= 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20
Technically, the first line is completely correct, but for clarity the second form
is usually used. We shall use the second form.
The first line is based on the fact that
102 = 210
102 = 210
9
2.2) Binary Number System
To add binary numbers, we need an addition table just as we need
an addition table to work with decimal numbers:
0 0 1 1
+0 +1 +0 +1
0 1 1 10
10
2.2) Binary Number System
Examples: Adding Binary Numbers
11
2.3) Hexadecimal Number System
Another number system that is commonly used for computer work
is the hexadecimal (or hex) number system. This system is based on
powers of 16.
In this system we need 16 distinct digits. Since we only have 10
digits from the decimal number system, letters are used for the rest:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
12
2.3) Hexadecimal Number System
When working with hexadecimal number it will be convenient to use
a table of equivalent values between the decimal and hex digits:
Decimal Hex Decimal Hex
0 0 8 8
1 1 9 9
2 2 10 A
3 3 11 B
4 4 12 C
5 5 13 D
6 6 14 E
7 7 15 F
13
2.3) Hexadecimal Number System
In the hex number system each position is a power of 16.
Example:
3A01 = 3 x 103 + A x 102 + 0 x 101 + 1 x 100
1016 = 1610
that is, 10 (base-16) = 16 (base-10). But for clarity we will use the second form.
14
2.3) Hexadecimal Number System
Again to add hex number we need an addition table. In this case it
would be 16x16. So it would be best to just look at some examples.
15
2.4) Number System Conversions
We can convert between number systems:
• Binary to Decimal
• Hex to Decimal
• Decimal to Binary
• Decimal to Hex
• Binary to Hex
• Hex to Binary
16
2.4.1) Binary to Decimal
To convert a binary number to a decimal number, we simply expand
using the base expressed in decimal:
Example:
110 = 1x22 + 1x21 + 0x20 = 4 + 2 + 0 = 6
So,
1102 = 610
17
2.4.2) Hex to Decimal
To convert a hex number to a decimal number, we simply expand
using the base expressed in decimal and the decimal equivalents for the
letter numbers from the table:
Example:
18
2.4.3) Decimal to Binary or Hex
There are two algorithms that can be used to convert from decimal
to binary or hex.
Method 1:
1) Divide the decimal number by 2 or 16 and record the multiples of 2
or 16 and the remainder in a table.
2) For hex, if the remainder is between 10 and 15, convert to
equivalent letter.
3) Go back to Step 1 and continue until the multiple is 0.
19
2.4.3) Decimal to Binary or Hex
Method 2:
1) Find the largest power of 2 or 16 that will go into the number.
2) Record the number of times this largest power goes into the number
and also the remainder.
3) Find largest power of 2 or 16 that will go into the remainder.
4) Go to step 2 and repeat under there are no more powers of 2 or 16.
20
2.4.3) Decimal to Binary or Hex
Examples:
21
2.4.4) Conversion between Binary and Hex
The computer works with binary numbers, but binary numbers can
get very long. Hex provides a convenient short-hand way to represent
binary numbers. In addition the conversion between binary and hex is
very simple.
You should note that hex is often used for computer work, but this is
merely a convenience for persons working with computers. The
computer works in binary.
22
2.4.4) Conversion between Binary and Hex
We will need a table for conversions between hex and binary
numbers. Note that it is essential to keep all the leading zeros in the
binary numbers.
Decimal Binary Hex Decimal Binary Hex
0 0000 0 8 1000 8
1 0001 1 9 1001 9
2 0010 2 10 1010 A
3 0011 3 11 1011 B
4 0100 4 12 1100 C
5 0101 5 13 1101 D
6 0110 6 14 1110 E
7 0111 7 15 1111 F
23
2.4) Conversion between Binary and Hex
Hex to Binary: Use the following algorithm:
1) Look up the binary equivalent for each hex digit number in the table.
2) Under the hex digit write the binary equivalent.
3) Group the binary number in sets of 4; pad the number with 0s on the left
if necessary to compete a set of 4.
4) Look up the hex digit corresponding to each binary group of four in the
table.
5) Under each binary group of four, write the hex digit.
24
2.4) Conversion between Binary and Hex
Examples:
25
2.5) Using a Calculator
Most scientific and engineering calculators today can do some limited
calculations using alternate bases. The common one are
Base-10
Base-2
Base-8 (Octal)
Base-16
You will usually have to enter a different mode on your calculator. You can
tell if your calculator can work in different number systems if you see some
keys with A, B, C, D, E, F which are used to enter hex numbers.
26
2.5) Using a Calculator
Some calculators will also have Base-5, for what reason I know not.
The Octal system is now obsolete, but was used when bytes were 6
binary digits. In this case the conversion was like the hex-binary
conversion but with sets of 3 bits. For example:
111 110
7 6
You are expected to be able to do the conversions in detail. You can use
your calculator to check you work, but on exams you are expected to
show all the details of the calculation or you will get zero (0).
27
2.6) Bytes
The smallest addressable unit of a computer is the byte:
1 byte = 8 bits
Since each 4 bits can be represented as one hex digit, it takes 2 hex digits to
represent a byte. For example (note the space is just for convenience of reading and
has no significance):
1111 0010 = F2
This is the reason we use hexadecimal numbers in computer work, because the 8 bit
byte can be represented the shorter 2 hex digits.
28
2.6) Bytes
Since a computer is fundamentally binary, it is usual to use powers
of 2 to represent sizes:
30
2.7) Summary
You are responsible for knowing the following:
31