ece 331 – digital system design representation and binary arithmetic of negative numbers and...

42
ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were taken from the materials accompanying Fundamentals of Logic Design, 6 th Edition, by Roth and Kinney, and were used with permission from Cengage Learning.

Upload: noreen-patrick

Post on 17-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

ECE 331 – Digital System Design

Representation and Binary Arithmeticof Negative Numbers

andBinary Codes

(Lecture #10)

The slides included herein were taken from the materials accompanying Fundamentals of Logic Design, 6th Edition, by Roth and Kinney,

and were used with permission from Cengage Learning.

Page 2: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 2

Representation of Negative Numbers

(continued)

Page 3: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 3

Signed Binary Numbers

Three representations for signed binary numbers:

1. Sign and Magnitude2. 1's Complement3. 2's Complement

Page 4: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 4

1's Complement

An n-bit positive number (P) is represented in the same way as in the Sign and Magnitude representation.

The sign bit (MSB) = 0. The remaining n-1 bits represent the magnitude.

Page 5: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 5

1's Complement

An n-bit negative number (N) is represented using the “1's Complement” of the equivalent positive number (P).

N' = 1's Complement representation for the negative number N.

N' = (2n – 1) – P where P = |N|

The sign bit (MSB) = 1 for all negative numbers using the 1's Complement representation.

Page 6: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 6

1's Complement

Example:

Determine the 1's Complement representation for the following negative numbers, using 8 bits:

- 11- 107- 74

Hint: (2n – 1) = (28 – 1) = 255

Page 7: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 7

1's Complement

The 1's Complement representation of N can also be determined using the bit-wise complement of P.

N = n-bit negative number P = |N| N' = 1's Complement representation of N. N' = bit-wise complement of P

i.e. complement P, bit-by-bit.

Page 8: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 8

1's Complement

Example:

Determine the 1's Complement representation (using the bit-wise complement) for the following

negative numbers, using 8 bits:

- 74- 11- 107

Page 9: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 9

1's Complement

For an n-bit signed binary number,

- (2n-1 – 1) <= D <= + (2n-1 – 1)

Includes a representation for -0 and +0. Represents an equal number of positive and

negative values.

Page 10: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 10

1's Complement

Given a negative number (N), represented using the 1's Complement representation (N'), the

magnitude of the number (P) can be determined as follows:

P = (2n – 1) – N'or

P = bit-wise complement of N'

Page 11: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 11

2's Complement

An n-bit positive number (P) is represented in the same way as in the Sign and Magnitude representation.

The sign bit (MSB) = 0. The remaining n-1 bits represent the magnitude.

Page 12: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 12

2's Complement

An n-bit negative number (N) is represented using the “2's Complement” of the equivalent positive number (P).

N* = 2's Complement representation for the negative number N.

N* = (2n) – P where P = |N|

The sign bit (MSB) = 1 for all negative numbers using the 2's Complement representation.

Page 13: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 13

2's Complement

Example:

Determine the 2's Complement representation for the following negative numbers, using 8 bits:

- 11- 107- 74

Hint: (2n) = (28) = 256

Page 14: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 14

2's Complement

The 2's Complement representation is related to the 1's Complement representation as follows:

N' = (2n – 1) – PN* = (2n) – P

N* = N' + 1

Page 15: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 15

2's Complement

The 2's Complement representation of N can also be determined by adding 1 to the 1's Complement representation of N.

N = n-bit negative number P = |N| N' = One's Complement representation of N.

N' = bit-wise complement of P. N* = N' + 1

Page 16: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 16

2's Complement

Example:

Determine the 2's Complement representation (using the 1's Complement) for the following

negative numbers, using 8 bits:

- 74- 11- 107

Page 17: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 17

2's Complement

For an n-bit signed binary number,

- (2n-1) <= D <= + (2n-1 – 1)

Includes only one representation for 0. Represents an additional negative value.

Page 18: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 18

2's Complement

Given a negative number (N), represented using the 2's Complement representation (N*), the

magnitude of the number (P) can be determined as follows:

P = (2n) – N*or

P = bit-wise complement of N* + 1

Page 19: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 19

Signed Binary Numbers

Page 20: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 20

Binary Arithmeticof

Signed Binary Numbers

Page 21: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 21

2's Complement Addition

Addition of n-bit signed binary numbers is straightforward using the 2's Complement system.

Addition is carried out in the same way as the addition of n-bit positive numbers.

Carry from the sign position (MSB) is ignored. Overflow occurs if the correct result (including

the sign) cannot be represented in n bits.

Page 22: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 22

2's Complement Addition

Example:

Page 23: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 23

2's Complement Addition

Example:

Page 24: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 24

2's Complement Addition

Example:

Page 25: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 25

2's Complement Addition

Exercise:

Add the following numbers using 2's Complement Addition:

32 + 45-17 + 63

Does overflow occur for either addition?

Page 26: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 26

Two's Complement Subtraction

Subtraction can be implemented using addition. Determine the 2's Complement representation

for the negative number -B. Use 2's Complement Addition to add A and -B.

A – B = A + (-B)

Page 27: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 27

2's Complement Subtraction

Exercise:

Subtract the following numbers using 2's Complement Addition:

32 – 45 -17 – 63

Does overflow occur for either subtraction?

Page 28: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 28

1's Complement Addition

Similar to the addition of n-bit numbers using 2's Complement Addition.

Instead of discarding the carry from the sign position (MSB), it must be added to the least significant bit (LSB) of the n-bit sum.

Referred to as an end-around carry.

Page 29: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 29

1's Complement Addition

Example:

Page 30: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 30

1's Complement Addition

Example:

Page 31: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 31

1's Complement Addition

Exercise:

Add the following numbers using 1's Complement Addition:

32 + 45-17 + 63

Does overflow occur for either addition?

Page 32: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 32

Overflow

General rule for detecting overflow when adding two n-bit numbers using either 1's Complement or 2's Complement Addition

An overflow occurs when the addition of two positive numbers results in a negative value or the addition of two negative numbers results in a positive value.

Cannot occur when adding a positive number and a negative number.

Page 33: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 33

Binary Codes

Page 34: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 34

Binary Codes

Weighted Codes Each position in the code has a specific

weight Decimal value of code can be determined

Unweighted Codes Positions of code do not have a specific

weight Decimal value assigned to each code

Page 35: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 35

Binary Codes

4-bit Weighted Codes Code: a3a2a1a0 Weights: w3, w2, w1, w0 Decimal Value: a3 x w3 + a2 x w2 + a1 x w1 + a0 x w0

Examples 8-4-2-1 6-3-1-1 Excess-3 (obtained from 8-4-2-1)

Page 36: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 36

Binary Codes

Examples of unweighted codes 2-out-of-5 Code

Exactly 2 of the 5 bits are “1” for a valid code word.

Gray Code Code values for successive decimal digits differ

in exactly one bit.

Page 37: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 37

Decimal Digit

8-4-2-1 Code (BCD)

6-3-1-1 Code

Excess-3 Code

2-out-of-5 Code

Gray Code

0 0000 0000 0011 00011 0000

1 0001 0001 0100 00101 0001

2 0010 0011 0101 00110 0011

3 0011 0100 0110 01001 0010

4 0100 0101 0111 01010 0110

5 0101 0111 1000 01100 1110

6 0110 1000 1001 10001 1010

7 0111 1001 1010 10010 1011

8 1000 1011 1011 10100 1001

9 1001 1100 1100 11000 1000

Binary Codes

Page 38: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 38

Binary Coded Decimal (BCD)

4-bit binary number used to represent each decimal digit.

Weighted code: 8-4-2-1 Binary values 0000 … 1001 used to represent

decimal values 0 … 9. Binary values 1010 … 1111 not used. Very different from binary representation.

Page 39: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 39

In the simplest form of binary code, each decimal digit is replaced by its binary equivalent. For example, 937.25 is represented by:

Binary Coded Decimal

Page 40: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 40

Binary Codes

ASCII Code American Standard Code for Information Interchange Common code used for the storage and transfer of

alphanumeric characters. 7-bit Weighted Code

Can represent a total of 128 characters Used to represent letters, numbers and other characters

(e.g. special control characters) Any word or number can be represented (and stored or

transferred) using its ASCII Code.

Page 41: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 41

ASCII Code (incomplete)

Page 42: ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were

Fall 2010 ECE 331 - Digital System Design 42

Questions?