2 numbersystemutmnotes
2 numbersystemutmnotes
Number Systems
1
Digital Number Systems
• Many number systems are used in digital
electronics.
– Decimal number system
– Binary number system
– Octal number system
– Hexadecimal number system
2
Decimal System
3
Decimal System
• The position of each digit in a decimal
number can be assigned a weight
• For example: 2745.214
– 2745.214 is a decimal number
– 2 is a digit, 7 is a digit, 4 is a digit…
2 7 4 5 . 2 1 4
4
Decimal System
2 7 4 5 . 2 1 4
2745.21410 =
(2x103) + (7x102) + (4x101) + (5x100) + (2x10-1) + (1x10-2)
+ (4x10-3)
5
Decimal System
2 7 4 5 . 2 1 4
6
Decimal Counting
• 0123456789-----
1011----------------------------19----20----
-------------------------------99----100------------
------
7
Binary System
8
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…
1 0 1 1 . 1 0 1
9
Binary System
weights 23 22 21 20 2-1 2-2 2-3
1 0 1 1 . 1 0 1
10
Binary counting
• 011011100101110 111--------
---------11111
11
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 number
• Widely used in computer and
microprocessor applications
12
Hexadecimal System
• Examples: 1C16 , A8516
• 1CH, A85H
• The position of each digit in a hexadecimal
number can be assigned a weight
• For example: 2AF8.98E
– 2AF8.98E is a hexadecimal number
– 2 is a digit, A is a digit, F is a digit…
weights 163 162 161 160 16-116-216-3
2 A F 8 . 9 8 E
13
Hexadecimal Counting
• 0123456789ABC
DEF101112-------------------
191A---------------------------------------
FF100101102------------------
14
Octal System
• Base-8 system
• 8 digits: 0,1,2,3,4,5,6,7
• Convenient way to express binary numbers
and codes. Use 3 bits of binary
15
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
16
Review
• Give the largest decimal number that can be
represented in binary with 8 bits.
• Determine the weight of the 1 in 100002.
• Convert the binary number 10111101.0112 to
decimal. Ans:189.37510
17
Conversions
• BinaryDecimal
• DecimalBinary
• HexadecimalDecimal
• DecimalHexadecimal
• BinaryHexadecimal
• HexadecimalBinary
• OctalDecimal
• DecimalOctal
• BinaryOctal
• OctalBinary
18
BINARY number DECIMAL number
Method 1: Sum-of-Weights
Given 110012
Binary Binary
S1: write the weights
weights 24 23 22 21 20
?
Binary number 1 1 0 0 1
110012 =(1x24)+(1x23)+(0x22)+(0x21)+(1x20)
=16 + 8 + 1
=2510
Decimal
Decimal Answer: 2510
19
Decimal Binary Conversion
• Decimal number binary number
– Method: sum-of-weights
• Decimal whole number binary number
– Method: division-by-2
• Decimal fraction binary number
– Method: multiplication-by-2
20
DECIMAL number BINARY number
Method 1: Sum-of-Weights
Decimal Given 2510
Step 1: Find the power of two that fulfills the following:
a. nearest to the given decimal number; and
b. its decimal number is less than or equal to the given decimal number.
22? No, because it is not the nearest. 8 (23) is nearer and still less than 25.
25? No, because its decimal number, 32 is more than 25.
24? Yes, because it is the nearest and its decimal, 16 is less than 25.
Step 2: Subtract the power of two (from Step 1) from the given decimal number.
25-16=9
The result of the subtraction is 9.
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.
Binary 21
DECIMAL Number BINARY Number
Method 1: Sum-of-Weights
Given 5110
• Step 1: the power of two which is nearest to 51 but less than
51 is 25
• Step 2: the result of subtraction 51-32=19
• Step 3:
– Repeat Step 1: the power of two nearest to 19 but less than 19 is 24
– Repeat Step 2: the result of subtraction 19-16=3
– Repeat Step 1: the power of two which is nearest to 3 and less than 3 is 21
– Repeat Step 2: the result of subtraction 3-2=1
– Repeat Step 1: the power of two which is nearest to 1 and equal to 1is 20
– Repeat Step 2: the result of subtraction 1-1=0
• Step 4: Weights 25 24 23 22 21 20
Binary number 1 1 0 0 1 1
22
DECIMAL Number BINARY Number
Method 2: Repeated Division-by-2
Quotient
• Method 1 can convert both Remainder
0.25 x 2 = 0.50 0
hexadecimal 3 F 1 6 9
The hexadecimal number is 3F16916
26
HEXADECIMAL Number BINARY Number
Hexadecimal C F 8 E
octal 6 5
The octal number is 658
29
OCTAL Number BINARY Number
• Step: Replace each octal digit with the
equivalent 3-bit group.
• Example: 138
Octal 1 3
30
Exercise
• Fill in the blanks.
Decimal Binary Hexadecimal Octal
1101.0112
10101.112
245.62510
70310
A8516
31
Exercise
• Answers
Decimal Binary Hexadecimal Octal
32
Binary Arithmetic
• Binary addition
• Binary subtraction
• Binary multiplication
• Binary division
Binary addition
• 0 + 0 = 0 with a carry of 0
• 0 + 1 = 1 with a carry of 0
• 1 + 0 = 1 with a carry of 0
• 1 + 1 = 10 with a carry of 1
• Example: 111 + 11 = ?
In decimal…
111 7
+ 11 +3
1010 10
Binary subtraction
• 0–0=0
• 1–1=0
• 1–0=1
• 10 – 1 = 1
• Example: 101 – 11 = ?
In decimal
101 5
- 11 -3
10 2
Binary Multiplication
• 0x0=0
• 0x1=0
• 1x0=0
• 1x1=1
• Example: 101 x 111 = ?
101 5
x 111 x7
101 35
101
101
100011
Binary Division
• The procedure is same as decimal division
• Examples: 110 ÷ 11 =?
In decimal…
10 2
11 110 3 6
11 6
0 0
0
Review
• Why binary numbers? Why not decimal?
– Digital system understand binary numbers, not
decimal numbers.
Review
• Why hexadecimal?
– Shorthand of binary numbers
– Easy to convert to and from binary
• Examples:
– 1 1111 1000 0000 1010 1000 00012
– 1F80A8116
Chapter 2
Number Systems
40
Chapter 2
Number Systems
41
Outline
• Binary Codes
– BCD 8421
– Gray
– ASCII
– EBCDIC
Code and Encoding
• 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.
Generally,
Number,
Letter, encoding Code
Word
For example,
Numbers, Binary
Letters, Straight binary coding
number
Words
Other Codes
• External world is decimal but digital systems
use binary numbers.
– So, conversions are often.
• Other codes are introduced because the
conversion between the code and the decimal
number is easier.
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: 87410
Do you know why?
Decimal 8 7 4
1000 1111 is not a BCD
BCD 1000 0111 0100
Therefore, the BCD code for 87410 is 1000 0111 0100
BCD Code / 8421 Code
• Convert a BCD code to its decimal equivalent.
– Step 1: Break the BCD into 4-bit groups, starting
from LSB
– Step 2: Replace each 4-bit group with its
equivalent decimal
• Example: 0110 1000 0011 1001
6 8 3 9
• No weights assigned to 0
1
0000
0001
0000
0001
the bit positions 2 0010 0011
3 0011 0010
• Only a single bit change 4 0100 0110
from one code word to 5 0101 0111
1 0 1 1 0 Gray
GRAY TO BINARY CONVERSION
1 1 0 1 1 Gray
1 0 0 1 0 Binary
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.
ASCII Code
• American Standard Code for International
Interchange
• Used in computers and electronic equipment
1011001
processor
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
ASCII CODE
EBCDIC ALPHANUMERIC CODE
Extended Binary Coded Decimal Interchange Code
•8-bit character encoding
Character or ASCII-8 Binary EBCDIC Binary
Number
A 01000001 11000001
E 01000101 11000101
Z 01011010 11101001
0 00110000 11110000
1 00110001 11110001
5 00110101 11110101
EBCDIC
Chapter 2
Number Systems
56
Outline
• Representation of negative numbers
– Sign and magnitude
– 1’s complement
– 2’s complement
Representation of negative numbers
• Computer must 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)
Sign and Magnitude
• The sign bit—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.
• Example: Express the decimal number -39 as
an 8-bit number in the sign-magnitude.
3910=1001112= 0010 01112
The sign bit is 1. Therefore, -3910= 1010 01112
Sign and Magnitude
-7 +0
-6 1111 0000 +1
1110 0001
-5 +2 +
1101 0010
-4 1100 0011 +3 0 100 = + 4
01001101
1’s Complement
-0 +0
-1 1111 0000 +1
1110 0001
-2 +2 +
1101 0010
-3 1100 0011 +3 0 100 = + 4
+19 -19
Sign-magnitude
1’s complement
2’s complement
Comparison
Exercise:
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
Number Systems
73
Outline
• Arithmetic Operations of 2’s Complement
– Addition
– Subtraction
– Overflow
Discard Discard
carry carry
• Example: Overflow in 4-bit system
7 0111 • Carry in sign different
from carry out sign, a sign
+4 + 0100 that overflow occurs.
•Does overflow occur for
11 1011
this case if this is an 8-bit
Wrong sign Wrong magnitude system?
2’s Complement Overflow
Expected 0 1 1 1 Actual Expected 1 0 0 0 Actual
+5 0101 -7 1001
+ +3 0011 + -2 1110
+8 1000 -8 -9 1 0111 +7
Overflow Overflow
+5 0101 -3 1101
+ +2 0010 + -5 1011
+7 0111 +7 -8 1 1000 -8
No Overflow No Overflow
Example
Add two numbers in two’s complement
representation: (-35) + (+20) (-15)
Addition and Subtraction : 2’s Complement
Example
Add two numbers in two’s complement
representation: (-35) + (+20) (-15)
Solution
Carry 1 1 1
1 1 0 1 1 1 0 1
+ 0 0 0 1 0 1 0 0
----------------------------------
Result 1 1 1 1 0 0 0 1 -15
Overflow Condition
Example
Add two numbers in two’s complement
representation: (+127) + (+3) (+130)
Overflow Condition
Example
Add two numbers in two’s complement
representation: (+127) + (+3) (+130)
Solution
Carry 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1
+ 0 0 0 0 0 0 1 1
----------------------------------
Result 1 0 0 0 0 0 1 0 -126 (Error)
An overflow has occurred.