ECEG-3201-DLD-Lec_02_Number System

ECEG-3201 Digital Logic Design
Addis Ababa Institute of Technology
(AAIT) Department of Electrical and
Computer Engineering
Learning Outcomes
At the end of the lecture, students should get
familiarized with:






Decimal, Binary, Octal & Hexadecimal systems.
Conversion between number systems.
Binary Codes.
Binary Arithmetic.
Negative Numbers.
AAIT, Department of
Electrical and Computer
Engineering
2
Nebyu Yonas Sutri
Digital Number Systems

Many number systems are used in digital
electronics:




Decimal number system (base 10).
Binary number system (base 2).
Octal number system (base 8).
Hexadecimal number system (base16).
AAIT, Department of
Electrical and Computer
Engineering
3
Nebyu Yonas Sutri
Number Systems
AAIT, Department of
Electrical and Computer
Engineering
4
Nebyu Yonas Sutri
Decimal system




We use decimal numbers everyday.
It is a base-10 system.
10 symbols: 0,1, 2, 3, 4, 5, 6,7, 8, 9.
The position of each digit in a decimal
number can be assigned a weight.
AAIT, Department of
Electrical and Computer
Engineering
5
Nebyu Yonas Sutri
Decimal system



Most significant digit (MSD) - the digit that
carries the most weight, usually the left most.
Least significant digit (LSD) - the digit that
carries the least weight, usually the right most.
Take example: decimal number  2745.214
Weights
103
102
101
100
2
7
4
5
.
10-1
10-2
10-3
2
1
4
2  103  7  102  4  101  5  100  2  101  1  102  4  103
AAIT, Department of
Electrical and Computer
Engineering
6
Nebyu Yonas Sutri
Binary system




Difficult to design a system that works with 10
different voltage levels.
Solution is base-2 (binary) system.
2 digits/symbols: 0, 1
Examples: 0, 1, 01, 111, 101010
AAIT, Department of
Electrical and Computer
Engineering
7
Nebyu Yonas Sutri
Binary System


The position of each digit (bit) in a binary
number can be assigned a weight.
For example: 1011.101


1011.101 is a binary number.
1 is a digit, 0 is a digit, 1 is a digit…
weights
2-1 2-2 2-3
23 22 21 20
1
0
1
1
. 1
1
LSB
MSB
AAIT, Department of
Electrical and Computer
Engineering
0
8
Nebyu Yonas Sutri
Binary System (BIT)




It takes more digits in the binary system to
represent the same value in the decimal
system.
Examples:
710 = 1112
1010  102
A single binary digit is referred to as a bit.
8 bits make a byte.
AAIT, Department of
Electrical and Computer
Engineering
9
Nebyu Yonas Sutri
Binary System (BIT)





With N bits we have 2N discrete values.
For example, a 4-bit system can represent 24
or 16 discrete values.
The largest value is always 2N – 1.
For the 4-bit system, 24 – 1 = 1510.
The range of values for a 4-bit number is then
0 thru 15.
AAIT, Department of
Electrical and Computer
Engineering
10
Nebyu Yonas Sutri
Hexadecimal System




Base-16 system.
16 symbols: 10 numeric digits and 6
alphabetic characters.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Compact way of writing binary system.
Widely used in computer and microprocessor
applications.
AAIT, Department of
Electrical and Computer
Engineering
11
Nebyu Yonas Sutri
Hexadecimal System



Examples: 1C16 , A8516
The position of each digit in a hexadecimal
number can be assigned a weight.
For example: 2AF8.98E
Weights
AAIT, Department of
Electrical and Computer
Engineering
163
162
161
160
2
A
F
8
12
16-1 16-2 16-3
.
9
8
E
Nebyu Yonas Sutri
Octal System



Base-8 system.
8 digits: 0, 1, 2, 3, 4, 5, 6, 7
Convenient way to express binary numbers
and codes.
AAIT, Department of
Electrical and Computer
Engineering
13
Nebyu Yonas Sutri
Table of Number Systems
DECIMAL
BINARY
HEXADECIMAL
OCTAL
0
0000
0
0
1
0001
1
1
2
0010
2
2
3
0011
3
3
4
0100
4
4
5
0101
5
5
6
0110
6
6
7
0111
7
7
8
1000
8
10
9
1001
9
11
10
1010
A
12
11
1011
B
13
12
1100
C
14
13
1101
D
15
14
1110
E
16
15
1111
F
17
AAIT, Department of
Electrical and Computer
Engineering
14
Nebyu Yonas Sutri
Conversion between Numbers
5
Octal
(base 8)
6
9
10
Decimal
(base 10)
1
Binary
(base 2)
2
7
8
Hexadecimal
(base 16)
3
4
AAIT, Department of
Electrical and Computer
Engineering
15
Nebyu Yonas Sutri
1. Decimal to Binary

Method 1: Decimal number  binary number


Method 2: Decimal whole number  binary
number.


Method: sum-of-weights.
Method: division-by-2
Method 3: Decimal fraction  binary number

Method: multiplication-by-2
AAIT, Department of
Electrical and Computer
Engineering
16
Nebyu Yonas Sutri
Method 1: Sum of Weights

Step 1: Find the power of two that fulfills the
following:





Nearest to the given decimal number; and
Its decimal number is less than or equal to the given
decimal number.
Step 2: Subtract the power of two (from Step 1) from
the given decimal number.
Step 3: If the result of the subtraction in Step 2 is 0,
go to Step 4. Else, repeat Steps 1 and 2 for the result
of the subtraction in Step 2.
Step 4: Write out the binary number based on all the
powers of two from Step 1.
AAIT, Department of
Electrical and Computer
Engineering
17
Nebyu Yonas Sutri
Method 1: Sum of Weights
Example 1: Convert 2510 to binary
Step 1:
22 = 4? No, because it is not the nearest.
23 = 8 is nearer and still less than 25.
24 = 16 is the nearest and its decimal, 16 is less than 25.
25 = 32 No, because its decimal number, 32 is more than 25.
Step 2: the result of subtraction 25 – 16 = 9
Step 3: Repeat Step 1: the power of two which is nearest to 9 but less than 9 is 23 = 8
Repeat Step 2: the result of subtraction 9 – 8 = 1
Repeat Step 1: the power of two which is nearest to 1 and equal to 1 is 20 = 1
Repeat Step 2: the result of subtraction 1 – 1 = 0
Step 4: Write out the binary number based on all the powers of two from Step 1.
Weights
Binary number
AAIT, Department of
Electrical and Computer
Engineering
24 23 22 21 20
1
1
18
0
0
1
Nebyu Yonas Sutri
Method 2: Division-by-2



Method 2 is used to convert only whole
decimal numbers (no fraction) to binary.
Repeat the division of the decimal number
with 2 until the quotient is 0.
Remainder of each division determine the
binary number. First remainder represent the
LSB and the last remainder is the MSB.
AAIT, Department of
Electrical and Computer
Engineering
19
Nebyu Yonas Sutri
Method 2: Division-by-2
Example 1: Convert 2510 to binary
Quotient
Remainder
25
 12
2
1
12
6
2
LSB
6
3
2
0
3
1
2
1
0
2
AAIT, Department of
Electrical and Computer
Engineering
0
2510 =
1
MSB
1
1
0
0
1
1
20
Nebyu Yonas Sutri
Method 3: Multiplication-by-2



Method 3 is used to convert decimal fraction
only to binary.
Repeat the multiplication until the fractional
part of the product are all zeros.
The binary number is determined by the first
digit in the multiplication results.
AAIT, Department of
Electrical and Computer
Engineering
21
Nebyu Yonas Sutri
Method 3: Multiplication-by-2
Example 2: Convert 0.3125 to binary
Carry
0.3125 x 2 = 0.625
0.625
x 2 = 1.25
0
1
0.25
x 2 = 0.50
0
0.50
x 2 = 1.00
1
AAIT, Department of
Electrical and Computer
Engineering
MSB
The binary
fraction is:
.
0
1
0
1
LSB
22
Nebyu Yonas Sutri
2. Binary to Decimal

Only one method is used. That is the sum of
weight.
Example 3: Convert 1011.101 to decimal
2-1 2-2 2-3
23 22 21 20
1
0
1
1
. 1
0
1
=(1x23) + (0x22) + (1x21) + (1x20) + (1x2-1) + (0x2-2) + (1x2-3)
= 8 + 0 + 2 + 1 + 0.5 + 0 + 0.125
= 11.62510
AAIT, Department of
Electrical and Computer
Engineering
23
Nebyu Yonas Sutri
3. Decimal to Hexadecimal



Method used is repeated division by-16.
Repeat the division of the decimal number
with 16 until the quotient is 0.
Remainder of each division determine the
hex number. First remainder represent the
LSB and the last remainder is the MSB.
AAIT, Department of
Electrical and Computer
Engineering
24
Nebyu Yonas Sutri
Decimal to Hexadecimal
Example 4: Convert 65010 to hex number
Quotient
Remainder
(decimal)
Remainder
(hexadecimal)
650
 40
16
10
40
2
16
8
8
2
0
16
2
2
A
LSB
MSB
65010 = 2
AAIT, Department of
Electrical and Computer
Engineering
25
8
A
Nebyu Yonas Sutri
4. Hexadecimal to Decimal

Only one method is used. That is the sum of
weight.
Example 5: Convert A8516 to decimal number
162
161
160
A
8
5
= (A x 162) + (8 x 161) + (5 x 160)
= (10 x 256) + (8 x 16) + (5 x 1)
= 2560 + 128 + 5
= 2693
AAIT, Department of
Electrical and Computer
Engineering
26
Nebyu Yonas Sutri
5. Decimal to Octal



Method used is repeated division by-8.
Repeat the division of the decimal number
with 8 until the quotient is 0.
Remainder of each division determine the oct
number. First remainder represent the LSB
and the last remainder is the MSB.
AAIT, Department of
Electrical and Computer
Engineering
27
Nebyu Yonas Sutri
Decimal to Octal
Example 6: Convert 35910 to octal number
Quotient
Remainder
359
 44
8
7
44
5
8
4
5
0
8
5
LSB
MSB
35910 = 5
AAIT, Department of
Electrical and Computer
Engineering
28
4
7
Nebyu Yonas Sutri
6. Octal to Decimal

Only one method is used. That is the sum of
weight.
Example 7: Convert 23748 to decimal number
83
82
81
80
2
3
7
4
= (2 x 83) + (3 x 82) + (7 x 81) + (4 x 80)
= (2 x 512) + (3 x 64) + (7 x 8) + (4 x 1)
= 1024 + 192 + 56 + 4
= 1276
AAIT, Department of
Electrical and Computer
Engineering
29
Nebyu Yonas Sutri
7. Binary to Hexadecimal


Step 1: Break the binary number into 4-bit
groups, starting from LSB.
Step 2: Replace each 4-bit with the
equivalent hexadecimal number.
Example 8: Convert 111111000101101001 to hex number
Binary
Hexadecimal
AAIT, Department of
Electrical and Computer
Engineering
0011 1111 0001 0110 1001
3
F
1
30
6
9
Nebyu Yonas Sutri
8. Hexadecimal to Binary

Step: Replace each digit of the hexadecimal
number with the equivalent 4-bit binary
number.
Example 9: Convert CF8E16 to binary number
Hexadecimal
Binary
AAIT, Department of
Electrical and Computer
Engineering
C
F
8
E
1100 1111 1000 1110
31
Nebyu Yonas Sutri
9. Binary to Octal


Step 1: Break the binary number into 3-bit
groups, starting from LSD.
Step 2: Replace each 3-bit group with the
equivalent octal number.
Example 10: Convert 1011110012 to octal number
Binary
101
111
001
Octal
5
7
1
AAIT, Department of
Electrical and Computer
Engineering
32
Nebyu Yonas Sutri
10. Octal to Binary

Step: Replace each digit of the octal number
with the equivalent 3-bit binary number.
Example 11: Convert 75268 to binary number
Octal
Binary
AAIT, Department of
Electrical and Computer
Engineering
7
5
2
6
111
101
010
110
33
Nebyu Yonas Sutri
Summary of Conversion
Division-by-8
Octal
(base 8)
Sum of weight
3 bit conversion
Digit-to-3 bit
Decimal
(base 10)
Division-by-2
Sum of weight
Binary
(base 2)
4 bit conversion
Digit-to-4 bit
Division-by-16
Hexadecimal
(base 16)
Sum of weight
AAIT, Department of
Electrical and Computer
Engineering
34
Nebyu Yonas Sutri
Binary Codes


Code  Special group of symbols that is used
to represent numbers, letters or words.
Encoding  The process of converting a
number/letter/word into a code.
Number,
Letter,
Word

encoding
Code
Codes going to be discussed:



BCD Code
Gray Code
ASCII Code
AAIT, Department of
Electrical and Computer
Engineering
35
Nebyu Yonas Sutri
BCD Code


Binary Coded Decimal Code
Binary Coded Decimal (BCD): a way to
represent each digit of a decimal number with
its 4-bit binary number.
Example 12 : Code the decimal number 87410 to a BCD Code
Decimal
8
7
4
BCD
1000
0111
0100
Therefore, the BCD code for 87410 is 1000 0111 0100
Do you know why?
1000 1111 is not a BCD
AAIT, Department of
Electrical and Computer
Engineering
36
Nebyu Yonas Sutri
BCD Code

Convert a
equivalent.


BCD
code
to
its
decimal
Step 1: Break the BCD into 4-bit groups, starting
from LSB.
Step 2: Replace each 4-bit group with its equivalent
decimal.
Example 13 : Convert the BCD code 0110 1000 0011 1001 to its decimal number.
0110
1000
0011
1001
6
8
3
9
So, the decimal equivalent is 683910
AAIT, Department of
Electrical and Computer
Engineering
37
Nebyu Yonas Sutri
BCD coding vs. Straight Binary coding
BCD coding is easier and straight forward.

Example 14 : Convert the decimal number 137.
Decimal
Binary
0
0000
1
0001
2
0010
3
0011
4
0100
5
0101
6
0110
7
0111
8
1000
9
1001
1
0001
3
0011
7
0111
The BCD code is 0001 0011 0111
AAIT, Department of
Electrical and Computer
Engineering
137
2
68
2
34
2
= 68
1
= 34
0
= 17
0
17
2
8
2
=8
1
=4
0
4
2
=2
0
2
2
1
2
=1
0
=0
1
The straight binary code is 10001001
38
Nebyu Yonas Sutri
Gray Code



No weights assigned
to the bit positions.
Only a single bit
change from one
code word to the
next in sequence.
Good  minimize
the chance for error.
AAIT, Department of
Electrical and Computer
Engineering
39
DECIMAL
BINARY
GRAY CODE
0
0000
0000
1
0001
0001
2
0010
0011
3
0011
0010
4
0100
0110
5
0101
0111
6
0110
0101
7
0111
0100
8
1000
1100
9
1001
1101
10
1010
1111
11
1011
1110
12
1100
1010
13
1101
1011
14
1110
1001
15
1111
1000
Nebyu Yonas Sutri
Binary to Gray code Conversion

Conversion from binary code to gray code
conversion is important.


Step 1: The most significant bit (left most) in the gray
code is same as the corresponding MSB in the
binary number.
Step 2: going from left to right, add each adjacent
pair of binary code bits to get the next gray code bit.
Discard caries.
AAIT, Department of
Electrical and Computer
Engineering
40
Nebyu Yonas Sutri
Gray code to Binary Conversion

Conversion from gray code to binary code is
carried as;


Step 1: The most significant bit (left most) in the
binary code is same as the corresponding bit in the
gray code.
Step 2: Add each binary code bit generated to the
gray code bit in the next adjacent position. Discard
caries.
AAIT, Department of
Electrical and Computer
Engineering
41
Nebyu Yonas Sutri
Alphanumeric Codes




Codes that represent numbers and
alphabetic characters (letters).
At minimum, the code must represent 10
decimal digits (0-9) and 26 letters (A-Z).
6 bits are needed in the code that represent
the numbers and letters.
ASCII is the most common alphanumeric
code.
AAIT, Department of
Electrical and Computer
Engineering
42
Nebyu Yonas Sutri
ASCII Code


American Standard Code for Information
Interchange.
Used in computers (keyboard and printers)
and electronic equipment.
1011001
processor
AAIT, Department of
Electrical and Computer
Engineering
43
Nebyu Yonas Sutri
ASCII Code




ASCII code has 128 characters and symbols
Represented by 7-bit binary code
Can be considered an 8-bit code with MSB 0.
The first 32 ASCII characters are non-graphic
commands only for control purposes - The
ASCII Control Characters.

E.g.: null, line feed, start of text, escape
AAIT, Department of
Electrical and Computer
Engineering
44
Nebyu Yonas Sutri
Binary Arithmetic




Binary Addition.
Binary Subtraction.
Binary Multiplication.
Binary Division.
AAIT, Department of
Electrical and Computer
Engineering
45
Nebyu Yonas Sutri
Binary Addition

Two binary numbers are added by adding
each pair of bits together with carry
propagation.




0+0=0
0+1=1
1+0=1
1 + 1 = 10
AAIT, Department of
Electrical and Computer
Engineering
with a carry of 0
with a carry of 0
with a carry of 0
with a carry of 1
46
Nebyu Yonas Sutri
Binary Addition
Example 15:
1 0 0 0
+
1 1 1
1 1 1 1
8
+ 7
15
Example 16:
Carry
+
AAIT, Department of
Electrical and Computer
Engineering
1 1
1 1
1 1
1 1 0
3
+ 3
6
47
Nebyu Yonas Sutri
Binary Subtraction

Two binary numbers are subtracted by
subtracting each pair of bits together with
borrowing, if needed.




0–0=0
1–1=0
1–0=1
10 – 1 = 1
AAIT, Department of
Electrical and Computer
Engineering
48
Nebyu Yonas Sutri
Binary Subtraction
Example 17:
1 0 1
1 1
1 0
5
- 3
2
Example 18:
Borrow
-
0 1 1
0 1 1
0 1
1 1 0 1 0 0 1 0
0 1 1 0 1 1 0 1
0 1 1 0 0 1 0 1
AAIT, Department of
Electrical and Computer
Engineering
49
210
- 109
101
Nebyu Yonas Sutri
Binary Multiplication

The procedure
multiplication




is
same
as
decimal
0x0=0
0x1=0
1x0=0
1x1=1
AAIT, Department of
Electrical and Computer
Engineering
50
Nebyu Yonas Sutri
Binary Multiplication
Example 19:
x
1
1 0
1 0 0
AAIT, Department of
Electrical and Computer
Engineering
1
1
1
0
1
0
0 1
1 1
0 1
1
5
x 7
35
1 1
51
Nebyu Yonas Sutri
Binary Division


Step 1. Align the divisor (Y) with the most
significant end of the dividend. Let the portion
of the dividend from its MSB to its bit aligned
with the LSB of the divisor be denoted X.
Step 2. Compare X and Y.



a) If X >= Y, the quotient bit is 1 and perform the
subtraction X-Y.
b) If X < Y, the quotient bit is 0 and do not perform
any subtractions.
Step 3. Shift Y one bit to the right and go to
step 2.
AAIT, Department of
Electrical and Computer
Engineering
52
Nebyu Yonas Sutri
Binary Division
Example 20:
AAIT, Department of
Electrical and Computer
Engineering
1 0
1 1 1 1 0
1 1
0
53
3
2
6
6
0
Nebyu Yonas Sutri
Negative Numbers



Computer must be handle both positive and
negative numbers.
A signed binary number consists of both sign
and magnitude information.
3 types of representation:



Sign and magnitude (least used).
1’s complement.
2’s complement (most important).
AAIT, Department of
Electrical and Computer
Engineering
54
Nebyu Yonas Sutri
Sign and Magnitude

The sign bit, i.e. the left-most bit in a signed
binary number.



A ‘0’ sign bit indicates a positive number.
A ‘1’ sign bit indicates a negative number.
The remaining bits are the magnitude bits.
AAIT, Department of
Electrical and Computer
Engineering
55
Nebyu Yonas Sutri
Sign and Magnitude
Example 21 : Express the decimal number -39 as an
8-bit number in the sign-magnitude.
First: Convert 3910 to binary =1001112
Second: Add a zero to as the 7th bit = 01001112
Since the decimal is a negative number, the sign bit is 1.
Therefore, -3910=101001112
AAIT, Department of
Electrical and Computer
Engineering
56
Nebyu Yonas Sutri
Sign and Magnitude
-7
-6
-5
1111
1110
+0
+1
0000
0001
1101
-4
-3
+2
0010
+3
1100
0011
1011
0100 +4
-2 1010
1001
-1
1000
-0
AAIT, Department of
Electrical and Computer
Engineering
0111
0101 +5
0110
+6
+
0 100 = + 4
1 100 = - 4
-
+7
57
Nebyu Yonas Sutri
1’s Complement

To find the 1’s complement of a given binary
number,

Change all 1s to 0s and all 0s to 1s.
Example 22 : Find the 1’s complement of 10110010
1 0 1 1 0 0 1 0
0 1 0 0 1 1 0 1
AAIT, Department of
Electrical and Computer
Engineering
58
1’s complement
Nebyu Yonas Sutri
1’s Complement
-0
-1
-2
111 1
111 0
+0
000 0
+1
000 1
110 1
001 0
+2
+
-3
110 0
001 1
+3
0 100 = + 4
-4
101 1
010 0
+4
1 011 = - 4
-5
101 0
010 1
100 1
-6
+5
-
011 0
100 0
-7
AAIT, Department of
Electrical and Computer
Engineering
011 1
+6
+7
59
Nebyu Yonas Sutri
2’s Complement

To find the 2’s complement of a given binary
number,

Add 1 to the LSB of the 1’s complement.
Example 23 : Find the 2’s complement of 10110010
1 0 1 1 0 0 1 0
Binary number
0 1 0 0 1 1 0 1
1’s complement
+
add 1
1
2’s complement
0 1 0 0 1 1 1 0
AAIT, Department of
Electrical and Computer
Engineering
60
Nebyu Yonas Sutri
2’s Complement
-1
-2
-3
111 1
111 0
+0
000 0
+1
000 1
110 1
001 0
110 0
001 1
+3
0 100 = + 4
-5
101 1
010 0
+4
1 100 = - 4
101 0
010 1
100 1
-7
+5
-
011 0
100 0
-8

+
-4
-6

+2
011 1
+6
+7
Only one representation for 0.
One more negative number than positive
number.
AAIT, Department of
Electrical and Computer
Engineering
61
Nebyu Yonas Sutri
Comparison
Example 24: Express the decimal numbers +19 and -19
as an 8-bit number in the sign-magnitude, 1’s
complement and 2’s complement forms.
+19
-19
Sign-magnitude
00010011
10010011
1’s complement
00010011
11101100
2’s complement
00010011
11101101
Positive number remain the same
AAIT, Department of
Electrical and Computer
Engineering
62
Nebyu Yonas Sutri
Comparison
AAIT, Department of
Electrical and Computer
Engineering
63
Nebyu Yonas Sutri
What to do this week?

Reading assignment.


Digital Design Principles and Practices, John F.
Wakerly, Chapter 2, Pages 21 - 74
Digital Fundamentals, Thomas L. Floyd, Chapter 2,
Pages 46 – 95.
AAIT, Department of
Electrical and Computer
Engineering
64
Nebyu Yonas Sutri