0% found this document useful (0 votes)
36 views

Lecture 5 - 6complements Codes

Uploaded by

Defenders
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lecture 5 - 6complements Codes

Uploaded by

Defenders
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

Digital Logic Design

Signed and Unsigned Data Representation)

Instructor: Muhammad Zeshan Qurashi

Department of Computing
Shifa Tameer-e-Millat University
(STMU). 11
Signed and Unsigned Numbers

• Binary numbers in digital computers can


be represented as:
• Unsigned Numbers
– Always positive
– No extra bit is required to denote sign (+ or -)
• Signed Numbers
– Numbers which can be positive as well as
negative
– Most significant bit (MSB) denotes the sign of
number
• MSB=0 , Positive Number
• MSB=1, Negative Number
2
Signed Data Representation

• Two approaches exist to represent signed numbers


1. Sign-Magnitude Representation
2. 2’s Compliment Representation

3
Signed Data Representation

1. Sign-Magnitude (S-M) Representation


• Most significant bit (MSB) represents sign
• MSB=0 , Positive Number
• MSB=1, Negative Number
No weight
23 22 21 20
b4 b3 b2 b1 b0

sign Magnitude

• The sign bit has no weight in S-M representation

4
Signed Data Representation

1. Sign-Magnitude (S-M) Representation

S-M Decimal S-M Decimal


0000 +0 1000 -0
0001 +1 1001 -1
0010 +2 1010 -2
0011 +3 1011 -3
0100 +4 1100 -4
0101 +5 1101 -5
0110 +6 1110 -6
0111 +7 1111 -7

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

2. Two’s Compliment Representation

• In almost all modern computers, signed data is represented in 2s compliment form


• The MSB not only signifies the sign of number but it also has a weight
• In an N-bit 2s Compliment Number, the sign bit has weight = −2𝑁−1
• The magnitude is calculated adding the weights of all non zero bits

−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

2. Two’s Compliment Representation


Binary Decimal Decimal Binary Decimal Decimal
value in SM value in 2s value in SM value in 2s
Compliment Compliment

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

Two’s Complement Representation

How to obtain a 2s Compliment of a Binary Number

• Obtain the 1s Complement of binary number by flipping all its bits


• Add the binary 1 to the LSB

Example:

8
Signed Data Representation

Two’s Complement Representation

How to obtain a 2s Compliment of a Binary Number

• Obtain the 1s Complement of binary number by flipping all its bits


• Add the binary 1 to the LSB

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

Given a number N in base-r having n digits, the (r-1)’s complement of N is


defined as (rn-1)-N.

Decimal numbers: 012398 have 6 digits and present below

(106 - 1) – 012398 = 999999 – 012398 = 987601

Binary numbers: 1011000=(88)10


(27 - 1) – 1011000 = 1111111 – 1011000 = 0100111

11
Radix complement

The r’s complement of an n-digit number in base-r is defined as rn - N, for

N=0 and 0 for N=0.


Compare with (r − 1)’s complement, the r’s complement is (r − 1)’s + 1 since

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

The subtraction of two n-digit unsigned numbers M − N in base-r


can be done as follows:

1. Add the minuend, M, to the r’s complement of the subtrahend, N.


This performs M + (rn − N) = M − N + rn.
2. If M≥N, sum will produce an end carry, rn, which can be discarded;
the result is M − N.
3. If M<N, the sum does not produce an end carry and is equal to r n −
(N − M). Take the r’s complement of the sum and place a negative
sign in front.

13
Examples of subtraction with 10’s
Complement
Using 10's complement. subtract 72532 - 3250.

Using l0's complement, subtract 3250 -


72532.

answer is -( 10's complement of 30718) = -


69282.

14
Examples of subtraction with 2’s Complement

X - Y = 1010100 - 1000011

Y - X = 1000011 – 1010100

the answer is Y - X = -( I's complement of 1101110 ) = -00100010.

15
Subtraction using (r-1)’s complement

(r − 1)’s complement is also used for subtraction


the sum is 1 less than the correct difference when an
end carry occurs
removing the end carry and adding 1 to the sum is
referred to as an end-around carry.

16
Examples of subtraction with 1’s Complement

X - Y = 1010100 - 1000011

Y - X = 1000011 – 1010100

the answer is Y - X = -( I's complement of 1101110 ) = -00100010.

17
Signed Binary Numbers

The convention is to make the sign bit 0 for positive and 1 for negative

In signed binary / signed magnitude,

leftmost bit -> sign

rest of the bits -> number.


In unsigned binary

leftmost bit -> most significant bit(MSB).


In 2’s complement could represent one more than negative number
because of no negative zero.

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)

• Arithmetic Operations on Signed Data


– Addition
– Subtraction
– Multiplication

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

• Maximum number in N-bits = 2N - 1


• Minimum Number ? 24
Signed Numbers Representation
• For signed numbers, sign information has to be
saved in 0’s and 1’s
• Signed Numbers can be represented as
– Sign Magnitude Form
– Complement Form
» 1’s Complement
» 2’s Complement
• All signed number representations use the most
significant bit (MSB) to represent the sign of the
number
– ‘0’  Positive Number
– ‘1’  Negative Number
25
Signed Magnitude Representation
• Magnitude does not change with sign. MSB is ‘0’ for
+ve numbers, ‘1’ for –ve numbers
S Magnitude (Binary)
(+3)10  ( 0 0 1 1 )2
(−3)10  ( 1 0 1 1 )2

Sign

Magnitude

• Issues with Sign Magnitude Representation


– Two representations for ‘+0’ and ‘-0’ 26
Signed Magnitude Representation:
Issues
Decimal Sign Mod.
• For a 4-bit binary
+7 0111
representation,
number range is +6 0110
-7 ≤ Number ≤ +7 +5 0101
+4 0100
• For n-Bit Representation +3 0011
+2 0010
+1 0001
−2n−1+1 ≤ Number ≤ +2n−1 − 1 +0 0000
–0 1000
–1 1001
• Sign Magnitude has –2 1010
two representations for ‘0’ –3 1011
–4 1100
– +0  0000 –5 1101
–6 1110
–-0 1000 –7 1111
9/21/2020 27
Signed Magnitude Representation :
Arithmetic Operations
• Sign Magnitude representation can’t include the sign
bit in Addition operation
0 0 1 1  (+3)10 1 0 0 1  (- 1)10
+ 1 0 1 1  (−3)10 + 0 1 0 1  (+5)10

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

• 1’s Complement is also used in logic operations


Complement of Complement of a Number
is original number
30
1’s Complement : Issues
Decimal 1’s Comp.
• For a 4-bit binary
+7 0111
representation,
number range is +6 0110
-7 ≤ Number ≤ +7 +5 0101
+4 0100
• For n-Bit Representation +3 0011
−2n−1+1 ≤ Number ≤ +2n−1 − 1 +2 0010
+1 0001
+0 0000
• 1’s Complement has –0 1111
two representations for ‘0’ –1 1110
–2 1101
– +0  0000 –3 1100
–4 1011
–-0 1111 –5 1010
–6 1001
–7 1000
31
2’s Complements
• 2’s Complement of a number is computed by :
– Take 1’s complement and add 1 to LSB
OR
– Toggle all bits to the left of the first ‘1’ from the
right
Example:
Number:
10110000 10110000
1’s Comp.:
01001111
+ 1

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)

1 Code (2’s Comp.)

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

• When subtracting A – B, convert B to its two's


complement
• Add A to (–B)

00001100 00001100
– 00000011 11111101
00001001

Practice: Subtract 0101 from 1001.

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

• Common Binary Codes


– BCD
– Excess 3
– Gray Code
– ASCII Code

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

• If a BCD digit is greater than 9 or 1001, it will


be invalid BCD Code
557
BCD Codes : Applications
• Real world data is in Decimal
• Computers/Digital Systems process data in
Binary
• BCD Codes are used in data display E.g. Seven
Segment Displays, LCD Displays

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

Two BCD Digits


58
BCD Addition

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

• In ASCII, each letter of the a 1100001


alphabet, punctuation marks, b 1100010
and numerous codes are . .
. .
assigned a unique 7-bit code
z 1111010
• Each ASCII code is stored in a
BYTE in computer memory @ 1000000
? 0111111
• Total Symbols = 27 = 128 + 0101011

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

You might also like