Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

2) Number Systems

Last update: 2021 September 14 Dr. E. Lang


© 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

= 1x103 + 2x102 + 3x101 + 3x100

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:

We have inherited features of this system: 60 seconds in a minute and 360


degrees in a circle.
4
2.1) Number Systems
It is not necessary to use a positional system, and in fact other systems
exist. For example, the Roman numeral system is an additive system, but a
smaller unit in front of a larger one is subtractive. The following are the
Roman numerals:

M = 1000 D = 500 C = 100


L = 50 X = 10
V=5 I=1
Example 2.2:

MMXVII = M+M+X+V+I+I = 1000 + 1000 + 10 + 5 + 1 + 1 = 2017

MCMLVX = M+(M-C)+L+(X-V) = 1000 + (1000 – 100) + 50 + (10 – 5) = 1955


5
2.1) Number Systems
A positional number system can be defined on any base; however,
with respect to computers, there are three important number systems:

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.

Note: For clarity, subscripts may be used to specify the base:

210 102 1516

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

Note: there is no single digit for 10 in the decimal system, since it is a


Base-10 system.

8
2.1.1) Decimal Number Systems
Example 2.3: Expand 120.56

Solution:

120.56 = 1 x 102 + 2 x 101 + 0 x 100 + 5 x 10-1 + 6 x 10-2

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

Note: Although we can use the tables for calculations in any


base, the method we will use is to use Base-10 for a side
calculation for all bases.

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

Note: there is no single digit for 2 in the binary system, since it is a


Base-2 system.

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

(= 1 x 10100 + 1 x 1010 + 0 x 101 + 1 x 100)

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:

Example 2.9: Convert 110 to 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:

Example 2.11: Convert A2C to decimal


Solution:

A2C = 10x162 + 2x161 + 12x160 = 2560 + 32 + 12 = 2604

 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

Example 2.13: Convert 502110 to binary using Method 2


 

27
2.3.3) Decimal to Binary or Hex
Example 2.14: Convert 5110 to Hex using Method 1

Example 2.15: Convert 274810 to Hex using Method 1


 
Example 2.16: Convert 502110 to Hex using Method 2
 

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.

Binary to Hex Algorithm:


3) Group the binary number in groups of 4; pad the number with 0s on
the left if necessary to compete a set of 4.
4) For each binary group, look up the hex digit in the table.
5) Write the hex digit each binary group.

31
2.3) Conversion between Binary and Hex
Example 2.17: Convert 111111111111 to Hex
 
Example 2.18: Convert 101010111100 to Hex

Example 2.19: Convert 32116 to binary

Example 2.20: Convert A36 to binary

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:

210 bytes = 1,024 bytes = 1 kB (kilobyte)


220 bytes = 1,048,576 bytes = 1 MB (megabyte)
230 bytes = 1,073,741,824 bytes = 1 GB (gigabyte)
240 bytes = 1,099,511,627,776 bytes = 1 TB (terabyte)

The number of bits is obtained by multiplying by 8 (1 byte = 8 bits).

Note: B  byte and b  bit.


37
2.6) Summary
In a positional number system, the position of a digit represents a
power of the base. There is nothing unique about the base and any
base can be chosen.
For computer work we use three number systems:

1) Decimal: the working base of humans


2) Binary: the working base of computers
3) Hexadecimal: a convenient shorthand for binary numbers

38
2.6) Summary
You are responsible for knowing the following:

• Understanding and being able to work in decimal, binary and


hexadecimal number systems.
• Be able to do addition in the three bases.
• Be able to convert positive integers between any number system.
• Be able to define bits, bytes and sizes.

If required, on exams you will be given, the table in Slide 30.

39

You might also like