Lecture 5 - 6complements Codes
Lecture 5 - 6complements Codes
Department of Computing
Shifa Tameer-e-Millat University
(STMU). 11
Signed and Unsigned Numbers
3
Signed Data Representation
sign Magnitude
4
Signed Data Representation
SM to Decimal Conversion
• The magnitude is calculated by adding the weights of all non zero bits except the sign bit
• If the sign bit is 0, the magnitude is positive else negative
5
Signed Data Representation
−24 23 22 21 20
b4 b3 b2 b1 b0
Example:
1 0 1 1 0
−24 + + 22 + 21 +0
0
= -16 + 4 + 1 = -11
0 0 1 1 0
0+ 0 + 22 + 21 +0 = +5 6
Signed Data Representation
0000 +0 +0 1000 -0 -8
0001 +1 +1 1001 -1 -7
0010 +2 +2 1010 -2 -6
0011 +3 +3 1011 -3 -5
0100 +4 +4 1100 -4 -4
0101 +5 +5 1101 -5 -3
0110 +6 +6 1110 -6 -2
0111 +7 +7 1111 -7 -1
7
Signed Data Representation
Example:
8
Signed Data Representation
Example:
9
Complements
Complements are used for simplifying the subtraction operation and for
logical manipulation.
There are two types of complements for each base-r system: the radix
and the diminished radix complements.
Binary numbers: 2’s complement
1’s complement
Decimal numbers:10’s complement
9’s complement
10
Diminished Radix Complement
11
Radix complement
rn − N=[(rn − 1) − N] + 1.
Decimal number: 012398
106 − 012398 = 987602 = 999999 − 012398 + 1
Binary number: 1011000
27 − 1011000=10000000-1011000=0101000=1111111 −
1011000+1
Leaving all least significant 0’s and
the first 1 unchanged, and others
have complemented
12
Subtraction with complements
13
Examples of subtraction with 10’s
Complement
Using 10's complement. subtract 72532 - 3250.
14
Examples of subtraction with 2’s Complement
X - Y = 1010100 - 1000011
Y - X = 1000011 – 1010100
15
Subtraction using (r-1)’s complement
16
Examples of subtraction with 1’s Complement
X - Y = 1010100 - 1000011
Y - X = 1000011 – 1010100
17
Signed Binary Numbers
The convention is to make the sign bit 0 for positive and 1 for negative
18
Signed Binary Numbers
19
20
Arithmetic Addition and Subtraction
2’s complement:
1. Have an end carry, discard then get answer
2. No end carry, find 2’s complement and add minus sign
front the answer
1’s complement:
3. Have an end around carry, add this bit then get answer
4. No end around carry, find 1’s complement and add minus
sign front the answer
21
Arithmetic Addition and Subtraction Cont…
22
Computer : Data Representation
• Data in computers is stored in binary format i.e.
0’s and 1’s
• Numbers in computers can be of Types
– Unsigned Numbers (Positive)
– Signed Numbers (Positive , Negative)
23
Unsigned Numbers
• Positive numbers/unsigned numbers are stored
in binary number format inside computers
• No of bits required to represent an unsigned
number depends on its magnitude
• Example
• 5 can be represented in 3 bits i.e. (101)2
• 10 can be represented in 4 bits i.e. (1010)2
• 25 can be represented in 5 bits i.e. (11001)2
Sign
Magnitude
1 1 1 0 (−6)10 1 1 1 0 (−6)10
• An appropriate format should represent the signed
data and handle arithmetic operations
• Solution Complements !!!
28
Compliment of a Number
• Computers use Complements to store signed
numbers (Positive and Negative)
– 1’s Complement ( r - 1 )
– 2’s Complement ( r)
• Applications of Complements
• Complements are used for subtraction inside
computers
• Complements can also be used in
operations logic
– 1’s complement is used for bit inversion
29
1’s Complement
• Complements used by Computers for
simplification
are of Subtraction operation
• 1’s Complement of number is obtained by
replacing
a
– All ‘0’s by ‘1’s
–All ‘1’s by ‘0’s
Example : (10110000)2
1’s Complement = (01001111)2
01010000 01010000
32
Signed Numbers :
2’s Complement Representation
• Computers store signed numbers in 2’s complement
binary format.
– Positive numbers are represented in binary
– Negative numbers are represented in 2’s Complement form
0 Magnitude (Binary)
Example
(+3)10 (0 011)2
(−3)10 (1 101)2
33
2’s Complement Representation :
Range
• For a 4-bit binary Decimal 2’s Comp.
representation, range is
number +7 0111
+6 0110
-8 ≤ Number ≤ +7 +5 0101
• For n-Bit Representation +4 0100
+3 0011
−2n−1 ≤ Number ≤ +2n−1 − 1 +2 0010
+1 0001
+0 0000
• Single representation for ‘0’
–1 1111
–2 1110
–3 1101
–4 1100
–5 1011
–6 1010
–7 1001
–8 1000
34
Range of Unsigned & Signed Numbers
• 4-Bit Numbers = 24 Decimal 2’s Comp.
+7 0111
• Unsigned No. Representation +6 0110
0 ≤ Number ≤ + 15 +5 0101
+4 0100
0 ≤ Number ≤ + 24 − 1 +3 0011
• n-Bit Representation +2 0010
0 ≤ Number ≤ + 2n − 1 +1 0001
+0 0000
• Signed No. Representation –1 1111
–2 1110
− 8 ≤ Number ≤ + 7
–3 1101
−23 ≤ Number ≤ + 23 − 1 –4 1100
• n-Bit Representation –5 1011
–6 1010
−2n−1 ≤ Number ≤ + 2n−1 − 1
–7 1001
–8 1000
35
Arithmetic Operations
( Addition,
Subtraction )
36
Binary Addition
• For binary addition
– Represent the numbers in binary form
– Add numbers bit by bit
– Propagate the carry, if required
(9)10 + (5)10
1001
+ 0101
1110 + 14
37
Binary Subtraction : On Paper
• Represent the numbers in binary form
• Subtract numbers bit by bit
• Borrow during binary subtraction has value of 2
• Example
(5)10 – (1)10 (8)10 –
(3)10
0101 1000
• Computer’s do - 0001 -
subtraction in 2’s 0011
Complement form
0+140 0 0+150 1
38
Binary Subtraction in Computers:
2’s Complement Arithematic
• Represent the numbers in binary form
• Take 2’s complement of 2nd no. and add into the 1st
no.
(5)10 – (1)10 (5)10 – (6)10
• If Carry = 1 (+5)10 + (-1)10 (+5)10 + (-6)10
ignore it, and the
result is 0101 0101
positive
+ 1111 + 1010
• If Carry = 0
Result is 0 1111
negative
1 0100
+4 –1
39
Practice Questions : Binary Numbers
• Compute 1’s Complement of following Numbers
» 0000 , 101011 , 01001011
• Compute 2’s Complement of following Numbers
» 0000 , 010101 , 11010010
• Compute 1’s and 2’s complements of the
following binary numbers:
» 11101010, 01111110 , 00000001,
10000000
• Subtract 2nd no. from 1st in each pair
» (010010 , 000111) ,
» (01010010, 00110100)
40
Practice Questions …
• Subtract the following unsigned binary numbers
using the 2’s-complement of the subtrahend.
Where result should be negative, 2’s
complement it and affix a minus sign
» 110100 – 10101
» 1011 – 110000
» 101010 – 101011
• Convert following numbers to binary and
compute result using 2’s complement
arithematic. Conver the answers back to decimal
» (37 – 61)
» (-55 + 70)
41
Binary Subtraction
00001100 00001100
– 00000011 11111101
00001001
42
Subtraction using Complements
43
Subtraction using Complements
44
Subtraction using Complements
45
46
47
48
49
50
51
52
53
Binary Codes
• Group of n bits
– Up to 2n unique Combinations
– Each combination represents an element of
information
54
5
4
Binary Coded Decimal (BCD)
• In BCD representation, each decimal digit is
represented by a 4 bit binary code
Decimal BCD
• BCD code for 8 is 1000 0 0000
• BCD code for 35 is 1 0001
2 0010
0011 0101 3 0011
4 0100
35 5 0101
6 0110
7 0111
0011 0101 8 1000
9 1001
56
Binary Coded Decimal (BCD)
• Binary Coded Decimal (BCD)
– BCD codes are used while displaying
decimal numbers in calculators, digital
number displays
57
BCD Addition
• If sum of two BCD Digits is ≤ 9, BCD addition is
like binary addition
• If sum of two BCD Digits is > 9, a
correction factor of 6 must be added
5 0101 7 0111
+ 3 + 0011 + 5 + 0101
8 1000 12 1100 Invalid
BCD Code
+ 0110
0001 0010
59
BCD Addition Example
60
BCD Subtraction
• If subtraction of two BCD Digits is ≤ 9,
BCD subtraction is like binary subtraction
• If subtraction of two BCD Digits is > 9,
a correction factor of 6 must be subtracted
8 1000 17 0001 0 1 1 1
- 2 - 0010 - 9 - 1001
6 0110 8 1110 Invalid
BCD Code
- 0110
1000
61
Practice Questions
• Convert the following to binary & BCD format
» 35
» 16
» 44
• Add the following numbers after converting them
into BCD format
» 35 + 77
» 29 + 57
• Subtract the following numbers after converting
them into BCD format
» 38 – 16
» 46 - 27
62
Excess-3 Code
• Excess-3 Code is Decimal Excess-3 Binary
obtained by adding 3 00 0011 0000
to each of the 01 0100 0001
decimal digit before 02 0101 0010
03 0110 0011
binary representation 04 0111 0100
• E.g. Excess-3 05 1000 0101
representation of 35 06 1001 0110
07 1010 0111
08 1011 1000
35 09 1100 1001
0110 1000
63
Gray Code / Reflected Code
• Gray Code is a Decimal Gray Binary
binary code in which 00 0000 0000
one bit changes from 01 0001 0001
02 0011 0010
one code to the next 03 0010 0011
code 04 0110 0100
• First and last code 05 0111 0101
06 0101 0110
also differ by one bit 07 0100 0111
(Cyclic Codes) 08 1100 1000
• E.g. 4-bit Gray codes 09 1101 1001
10 1111 1010
are shown in table 11 1110 1011
12 1010 1100
13 1011 1101
14 1001 1110
15 1000 1111
64
Gray Code Generation
• N-bit Gray code using Reflect and Prefix method
65
ASCII Code
• American Standard Code for Info 7-bit Code
Information Interchange (ASCII) A 1000001
B 1000010
• Computers use ASCII codes to . .
represent alphabets and special . .
characters (alphanumeric) Z 1011010
66
ASCII Codes Table
• a
Special
Function
Characters
67
Error Detecting Codes : Parity
• Parity
• One bit added to a group of bits to make the
total number of ‘1’s (including the parity bit)
even or odd. Good for checking Single bit
Errors in data
4-bit Data 7-bit Data
• Even Parity
1 0 1 1 1 0 1 0 0 0 0 0 1
• Odd Parity
0 0 1 1 1 1 1 0 0 0 0 0 1
68
ASCII Codes : Practice Questions
• Find ASCII Codes for following characters and
strings. Also Add Even parity bit to the ASCII
Codes of each character. Use the ASCII Codes
Table. Represent the codes in HEX
–K
–@
– DLD
– STMU
– [Your First Name]
69
Book : Practice Questions
• Attempt the following problems from Book
Chapter 1
– 1.3
– 1.9
– 1.11
– 1.14
– 1.18
– 1.23
– 1.28
70