Sie sind auf Seite 1von 54

Digital Logic Design (DLD)

Lecture 4
Outlines
➢ Compliments of Numbers
▪ Why Compliments (Comp.)?
▪ 10‘s comp. (or 2‘s comp.) radix (base) comp.
▪ 9‘s comp. (or 1‘s comp.) dimished radix comp.
▪ Subtraction with Comp.

➢ Representation of Binary Numbers


➢ Binary Codes
▪ BCD (Binary Coded Decimal)
▪ Gray Code
▪ ASCII Code
▪ Error-detection Code
Compliments of
Numbers
Why Complements?
1 2 = (10)2
0 2 2 0 0 2
1 0 0 1 1 0 1 = 77
− 1 0 1 1 1 = 23

0 1 1 0 1 1 0 = 54

➢ Borrow a “Base” (2 in binary) when needed


➢ However, for digital computers, its is more easy to
use compliments for:
❑ Simplifying subtraction procedure
❑ Logical manipulation
Types of Complements?
Type Decimal Binary

Dimished Radix Comp. 9’s Comp. 1’s Comp.


(r-1’s Comp.)

Radix (Base) Comp. 10’s Comp. 2’s Comp.


r’s Comp.
Diminshed Radix (r-1) ’s Complement
➢ 9’s Complement (of a decimal no.)
❑ Subtract each digit from 9

➢ 1’s Complement (of a binary no.)


❑ Subtract each digit from 1
❑ Given the fact: 1–0 =1 & 1–1=0,
❑ Means subtracting any digit from 1 → Inverting it
❑ Change 1’s to 0’s and 0’s to 1’s to get 1’s comp.
Radix (r’s) Complement
➢ 10’s Complement (of a decimal no.)
❑ 9’s Complement + 1

➢ 2’s Complement (of a binary no.)


❑ 1’s Complement + 1
❑ Leave all least significant 0’s and the first 1 unchanged and
replace all other 1’s with 0’s and 0’s with 1’s
Subtraction With r’s Complements
➢ Subtraction of two n‐digit unsigned numbers M-N
Match # of digits of M & N M
N
M + r’s comp. of N

Sum

YES
M≥N
carry is there Using 10’s Compl.,
Discard carry
Subtract
72532 - 3250

Result = M-N
Subtraction With 10’s Complements
➢ Subtraction of two n‐digit unsigned numbers M-N

Using 10’s Complement, Subtract 72532 - 3250


Subtraction With 2’s Complements
Subtraction With r’s Complements
➢ Subtraction of two n‐digit unsigned numbers M-N
Match # of digits of M & N M
N
M + r’s comp. of N

Sum

NO (M<N)
M≥N
No carry
Using 10’s Compl.,
Take r’s comp. of sum Subtract
Insert a -ive sign
3250 - 72532

Result = M-N
Subtraction With 10’s Complements
➢ Subtraction of two n‐digit unsigned numbers M-N

Using 10’s Complement, Subtract 3250 - 72532


Subtraction With 2’s Complements
Subtraction With r’s Comp.
(Overall Algo.)
Match # of digits of M & N

M + r’s comp. of N

Sum

YES NO (M<N)
M≥N
carry is there No carry

Discard carry Take r’s comp. of sum

Insert a -ive sign


Result = M-N
Result = M-N
Subtraction With (r-1)’s Complement
➢ Subtraction of two n‐digit unsigned numbers M-N

Subtraction of unsigned numbers can also be done by means


of the (r – 1)’s complement. Remember that the (r – 1)’s
complement is one less than the r’s complement. Because of
this, the result of adding the minuend to the complement of
the subtrahend produces a sum that is one 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.
Subtraction With 1’s Complement
➢ Subtraction of two n‐digit unsigned numbers M-N
Subtraction With 1’s Complement
➢ Subtraction of two n‐digit unsigned numbers M-N
Representation of
Binary Numbers
Representation of a Binary No.
➢ In digital System data is stored in form of binary
numbers
➢ There are three methods to represent the binary
numbers in digital systems:
❑ Unsigned representation
❑ Signed magnitude representation
❑ 2’s Complement representation
Unsigned Representation
➢ All binary numbers are considered positive
➢ What you have studied so far in Decimal → Binary
conversion
➢ Left bit is Most Significant Bit (MSB) of the
number
➢ (41)10 = (101001)2
Unsigned Representation
Signed Magnitude Representation
➢ Magnitude is magnitude, does not change with
sign
S Magnitude (Binary)

(+3)10  ( 0 0 1 1 )2
(−3)10  ( 1 0 1 1 )2
Sign Magnitude
Signed Magnitude Representation
➢ Left most bit represents sign while the rest of bits
represent the actual number (magnitude)
➢ Convention: Sign bit 0 for +ive and 1 for -ive
➢ User determines if the no. is signed or unsigned

Bin. → Dec.
25
Unsigned
(11001)2
-9
Left bit = 1 (means no. is –ive) Signed
Remaining no. 1001 (means 9)
Signed Magnitude Representation
0 indicating +ive no.

Same as before
(Simple binary)

Same as above BUT


with MSB = 1

1 indicating -ive no.


Signed Magnitude Representation
➢ Used in ordinary arithmetic
➢ However, awkward for computers because of
separate handling of sign and magnitude
➢ Motivation for 2’s Complements
2‘s Complement Representation
➢ More convenient representation to implement
arithmetic operations in computers
➢ +ive no. is represented in its original form
➢ -ive no. is represented by its 2’s Complement
+9 0000 1001
Unsigned
1000 1001
Signed Magnitude

-9 11110110
1’s Complement

11110111
2‘s Complement Representation

Same as before
(Simple binary)

2’s Complement of
above
Putting Everything Together
Binary Codes
Signed One’s Signed Two’s
Decimal Signed Magnitude
Complement Complement
+7 0111 0111 0111
+6 0110 0110 0110
+5 0101 0101 0101
+4 0100 0100 0100
+3 0011 0011 0011
+2 0010 0010 0010
+1 0001 0001 0001
+0 0000 0000 0000
-0 1000 1111 –
-1 1001 1110 1111
-2 1010 1101 1110
-3 1011 1100 1101
-4 1100 1011 1100
-5 1101 1010 1011
-6 1110 1001 1010
-7 1111 1000 1001
31/35
Bits Range
8 -270 (-128) to +27-1 (+127)
16 -215 (-32,768) to +215-1 (32,767)
-231 (-2,147,483,648) to +231-1
32
(+2,147,483,647)
-263 (-9,223,372,036,854,775,808) to
64
+263-1 (9,223,372,036,854,775,807

The largest positive value in N-bit two's complement is


0111...111, which is 2N-1-1.
The smallest negative value in N-bit two's complement is
1000...000, which is -2N-1. 32/35
Binary Codes
➢ Binary is used in machines
➢ However humans are more ‘comfortable’ with
decimal no. and alphabets
➢ How a sequence of decimals and alphabets will
be interpreted → Code
➢ An n-bit binary code is an n-bit word which can
represent up to 2n different elements
Binary Codes
➢ Examples:
❑ A binary code for the seven colors of Rainbow
Color Binary Number
Red 000
Orange 001
Yellow 010
Green 011
Blue 101
Indigo 110
Violet 111
➢ Standard Examples:
❑ Binary Coded Decimal (BCD)
❑ Gray Code
❑ ASCII Code
❑ Error Detecting Code
Binary Coded Decimal (BCD) Code
➢ BCD code is the 8,4,2,1 code
➢ This code only encodes the first ten values from 0
to 9
➢ Each decimal digit is coded separately by 4 bits
➢ Example:

(185)10

(0001 1000 0101)BCD

(10111001)2
Warning: Conversion or Coding?
➢ Do NOT mix up conversion of a decimal number
to a binary number with coding a decimal number
with a BINARY CODE
➢ Example:
❑ (13)10 = (1101)2 (This is conversion)
❑ (13)10  0001 0011 (This is coding)
Binary Coded Decimal (BCD) Code
Binary Coded Decimal (BCD) Code
➢ BCD is a weighted code (8421)
➢ Each bit position is assigned a weighting factor
such that each digit can be evaluated by adding
the weights of all 1’s in the coded combination
➢ Example:

(0110)

8X0 + 4X1 + 2X1 + 1X0

=6
2421 Weighted Code
➢ Left most bit: weightage of 2
➢ Right most bit: weightage of 1
8, 4, -2, -1 Weighted Code
Excess 3 Code
Binary + 3
Putting All Together
Gray Code
Decimal Binary Gray
00 0000 0000
01 0001 0001
02 0010 2 bit changes 0011
03 0011 0010
04 0100 3 bit changes 0110
05 0101 0111
06 0110 2 bit changes 0101 Only
07 0111 0100 1 bit
08 1000 1100 change
09 1001 1101
10 1010 1111
11 1011 1110
12 1100 1010
13 1101 1011
14 1110 1001
15 1111 1000

As we “count” up or down in decimal, the code word for the Gray


code changes in only one bit position as we go from decimal digit to
digit including from 9 to 0.
ASCII Code
➢ American Standard Code for Information
Interchange
➢ This code is a popular code used to represent
information sent as character-based data
➢ It is a 7-bit code used to represent:
❑ 94 Graphic printing characters
❑ 34 Non-printing characters
➢ Some non-printing characters are used for text
format. Examples:
❑ BS = Backspace
❑ CR = carriage return
7-bit code (B7B6B5 B4B3B2B1) ASCII Code
H=(1001000)
Error-Detecting Code
➢ Extra info. can be added in Binary code (data) to
detect errors
➢ ASCII is a 7-bit code
➢ Extra bit (called as parity) is appended at in the
most significant position
➢ To make the total number of 1’s either even or
odd
➢ Suppose ‘Even parity’ is used (more common)

7
TX RX
Parity bit
Error-Detecting Code
➢ Examples:

➢ In each case, the extra inserted bit (parity) leads


(in total) to even no. of 1’s
➢ Receiver (RX) knows this info. as well
➢ Read about “Odd parity” from book

7
TX RX
Parity bit
Error-Detecting Code
At the receiver side:
If an even parity is
7
detected, send an ACK
TX RX (Acknowledgement).
Parity bit If error was detected
send negative
Transmit Ha in ASCII acknowledge (NAK)

1001000 1100001 ASCII of “Ha”

Transmit with even parity:


01001000 11100001

added parity bit


Review Questions
• Why digital systems are more feasible?
• What language computers understand?
• The method for converting integer part of a decimal number to another base is
___________________.
• For conversion of fractional part of a decimal number ___________________ is used.
• Which of these can be directly converted to base-2 by grouping bits
– (i) Quinary (ii) Octal (iii) Decimal (iv) Hexadecimal
• What is the significance of complementary number and where do we apply it?
• What is diminished radix complement?
• What is the relationship between diminished radix complement and radix complement?
• What is the quick rule for finding 1’s complement?
• What is the quick rule for finding 2’s complement?
• What are the three notations used for representing signed binary numbers?
• Binary codes (i) BCD, (ii) ASCII (iii) Grey Codes
• What is the purpose of using Parity bit?
• What is a cell?
• What is a register?
• An “n” bit register can store how many different combinations?
• Binary arithmetic and binary logic are the same thing? T/F
• What three gates we have learned? Operator? Truth table? Symbol?
Thanks
Sikender Gul
Room 318/G-02 - ECE Deptt.
Gray Code (Continued)

▪ Does this special Gray code property


have any value?
▪ An Example: Optical Shaft Encoder
111 000 100 000
B0
B1
110 001 101 001
B2

G2
G1
111 G0
101 010 011

100 011 110 010


(a) Binary Code for Positions 0 through 7 (b) Gray Code for Positions 0 through 7
Chapter 1 51
Gray Code (Continued)

▪ How does the shaft encoder work?

▪ For the binary code, what codes may be


produced if the shaft position lies
between codes for 3 and 4 (011 and 100)?

▪ Is this a problem?

Chapter 1 52
Gray Code (Continued)

▪ For the Gray code, what codes may be


produced if the shaft position lies
between codes for 3 and 4 (010 and 110)?

▪ Is this a problem?

▪ Does the Gray code function correctly for


these borderline shaft positions for all
cases encountered in octal counting?
Chapter 1 53
Error Detecting Codes

Parity
One bit added to a group of bits to make the total number
of ‘1’s (including the parity bit) even or odd

4-bit Example 7-bit Example

● Even 1 0 1 1 1 0 1 0 0 0 0 0 1

● Odd 0 0 1 1 1 1 1 0 0 0 0 0 1

 Good for checking single-bit errors


54 / 45

Das könnte Ihnen auch gefallen