Professional Documents
Culture Documents
2) Number Systems: Dr. E. Lang
2) Number Systems: Dr. E. Lang
1
Table of Contents
2.1 Number Systems
2.1.2 Decimal Number System
2.1.2 Binary Number System
2.1.3 Hexadecimal Number System
2.3 Number System Conversions
2.3.1 Binary to Decimal
2.3.2 Hex to Decimal
2.3.3 Decimal to Binary or Hex
2.3.4 Conversion between Binary and Hex
2.4 Using a Calculator
2.5 Bytes
2.6 Summary
2
2.1) Number Systems
The number system we use is a positional number system where
value of each digit depends its position.
Example 2.1:
1233 = 1000 + 200 + 30 + 3
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:
1) Decimal (Base-10)
2) Binary (Base-2)
3) Hexadecimal, or Hex (Base-16)
6
2.1) Number Systems
The decimal number system (Base-10) is the one we commonly use.
But a computer is effectively a set of on/off switches; therefore, it is
convenient to represent data using a binary (Base-2) numbers.
Binary numbers can get very large; therefore, the hex (Base-16)
system is used as a shorthand for binary numbers.
7
2.1.1) Decimal Number Systems
(Lecture 3) In the decimal number system, each digit represents a
power of 10 and there are 10 distinct digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
8
2.1.1) Decimal Number Systems
Example 2.3: Expand 120.56
Solution:
9
2.1.1) Decimal Number Systems
To add decimal numbers, we need a 10x10 addition table,
which you learned in your early school years:
+ 0 1 2 3 4 5 6 7 8 9
0 0 1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9 10
2 2 3 4 5 6 7 8 9 10 11
3 3 4 5 6 7 8 9 10 11 12
4 4 5 6 7 8 9 10 11 12 13
5 5 6 7 8 9 10 11 12 13 14
6 6 7 8 9 10 11 12 13 14 15
7 7 8 9 10 11 12 13 14 15 16
8 8 9 10 11 12 13 14 15 16 17
9 9 10 11 12 13 14 15 16 17 18
10
2.1.1) Decimal Number Systems
Example 2.4: Decimal Addition
Add 83 + 78 + 97
11
2.1.2) Binary Number System
In the binary number system, each digit represents a power of 2 and
there are 2 distinct digits:
0, 1
12
2.1.2) Binary Number System
Example 2.5: Expand 1101
Solution:
1101 = 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20
For clarity and ease of use, the first form, which we will use, mixes binary (the
factor) and decimal (the power). But technically, the second line is the
correct form and is based on the fact that
102 = 210
13
2.1.2) Binary Number System
To add binary numbers, we need a 2x2 addition:
+ 0 1
0 0 1
1 1 10
14
2.1.2) Binary Number System
Example 2.6: Binary Addition
Add the following:
a) 1 + 1
b) 1110 + 1011
c) 111 + 111
15
2.2.3) Hexadecimal Number System
The hexadecimal (or hex) number system is based on powers of 16,
so we need 16 distinct digits. However, 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
Note: there is no single digit for 16 in the hex system, since it is a Base-
16 system.
16
2.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
17
2.2.3) Hexadecimal Number System
Example 2.7: Expand 3A01
Solution:
3A01 = 3 x 163 + A x 162 + 0 x 161 + 1 x 160
18
2.2.3) Hexadecimal Number System
To add hex numbers, we need a 16x16 addition table:
+ 0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 1 2 3 4 5 6 7 8 9 A B C D E F
1 1 2 3 4 5 6 7 8 9 A B C D E F 10
2 2 3 4 5 6 7 8 9 A B C D E F 10 11
3 3 4 5 6 7 8 9 A B C D E F 10 11 12
4 4 5 6 7 8 9 A B C D E F 10 11 12 13
5 5 6 7 8 9 A B C D E F 10 11 12 13 14
6 6 7 8 9 A B C D E F 10 11 12 13 14 15
7 7 8 9 A B C D E F 10 11 12 13 14 15 16
8 8 9 A B C D E F 10 11 12 13 14 15 16 17
9 9 A B C D E F 10 11 12 13 14 15 16 17 18
A A B C D E F 10 11 12 13 14 15 16 17 18 19
B B C D E F 10 11 12 13 14 15 16 17 18 19 1A
C C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B
D D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C
E E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D
F F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E
19
2.2.3) Hexadecimal Number System
Example 2.8: Hexadecimal Addition
Perform the hex addition for the following:
a) 1F + 23
b) 589 + 7B8 + 98A
c) FFFF + 5
20
2.3) Number System Conversions
We can convert between number systems:
Decimal Binary
Decimal Hex
Binary Hex
21
2.3.1) Binary to Decimal
To convert a binary number to a decimal number, we simply
expand using the base expressed in decimal:
Solution:
110 = 1x22 + 1x21 + 0x20 = 4 + 2 + 0 = 6
1102 = 610
22
2.3.3) Decimal to Binary or Hex
Example 2.10: Convert 1111 to a decimal number using 2
methods
23
2.3.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:
A2C16 = 260410
24
2.3.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 (16) and record the multiples of 2
(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.
25
2.3.3) Decimal to Binary or Hex
Method 2:
1) Find the largest power of 2 (16) that will go into the number.
2) Record the number of times this largest power goes into the number
and the remainder.
3) Find largest power of 2 (16) that will go into the remainder.
4) Go to step 2 and repeat under there are no more powers of 2 (16).
26
2.3.3) Decimal to Binary or Hex
Example 2.12: Convert 5110 to binary using Method 1
27
2.3.3) Decimal to Binary or Hex
Example 2.14: Convert 5110 to Hex using Method 1
28
2.3.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 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.
29
2.3.4) Conversion between Binary and Hex
We will need a table for conversions between hex and binary
numbers. Note: 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
30
2.3) Conversion between Binary and Hex
Hex to Binary Algorithm:
1) For each hex digit, look up the binary value in the table.
2) Under the hex digit write the binary value.
31
2.3) Conversion between Binary and Hex
Example 2.17: Convert 111111111111 to Hex
Example 2.18: Convert 101010111100 to Hex
32
2.4) 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-16
Base-8 (Octal)
You will usually have to enter a different mode on your calculator. If your
calculator works in different number systems, you see keys with A, B, C, D, E,
F used for hex.
33
2.4) Using a Calculator
The Octal (base-8) system is now obsolete, but was used when bytes
of older computers 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
34
2.4) Using a Calculator
In addition, there are many web sites that provide conversion
between number systems.
You are expected to be able to do the conversions in detail. You can
use your calculator to check you work, but on exams and assignments
you are expected to show all the details of the calculation or you will get
zero (0).
35
2.5) Bytes
The smallest addressable unit of a computer is the byte:
1 byte = 8 bits
Since 4 bits can be represented by 1 hex digit, it takes 2 hex digits to represent 1
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 by only 2 hex digits.
36
2.5) Bytes
Since a computer is fundamentally binary, it is usual to use powers of 2 to
represent sizes:
38
2.6) Summary
You are responsible for knowing the following:
39