Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Engineering (Logic Circuits) Dr. Tamer Samy Gaafar Lec. # 2

Similar presentations


Presentation on theme: "Computer Engineering (Logic Circuits) Dr. Tamer Samy Gaafar Lec. # 2"— Presentation transcript:

1 Computer Engineering (Logic Circuits) Dr. Tamer Samy Gaafar Lec. # 2
Dept. of Computer & Systems Engineering Faculty of Engineering Zagazig University

2 Course Web Page http://www.tsgaafar.faculty.zu.edu.eg

3 Announcements Sections will start this week (today)
A quiz will be held on the next week lecture (23/2/2015) at the 1st 20 mins. The quiz will be on ch.1 as a whole. No cheating is allowed (zero for both cheaters).

4 Lecture 2 Number Systems (cont.)

5 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2N bits = 0 to 15 8 bits = 0 to bits = 0 to 65535 Besides simply representation, we would like to also do arithmetic operations on numbers in binary form. Principal operations are addition and subtraction.

6 Binary Arithmetic, Subtraction
The rules for binary arithmetic are: The rules for binary subtraction are: 0 + 0 = 0, carry = 0 0 - 0 = 0, borrow = 0 1 + 0 = 1, carry = 0 1 - 0 = 1, borrow = 0 0 + 1 = 1, carry = 0 0 - 1 = 1, borrow = 1 1 + 1 = 0, carry = 1 1 - 1 = 0, borrow = 0 Borrows, Carries from/to digits to left of current of digit. Binary subtraction, addition works just the same as decimal addition, subtraction.

7 Binary, Decimal addition
101011 From LSB to MSB: 1+1 = 0, carry of 1 1 (carry)+1+0 = 0, carry of 1 1 (carry) = 1, no carry = = = 1 answer = 34 from LSD to MSD: 7+4 = 1; with carry out of 1 to next column 1 (carry) = 5. answer = 51.

8 Subtraction Decimal Binary 900 100
= 9; with borrow of 1 from next column 0 -1 (borrow) - 0 = 9, with borrow of (borrow) - 0 = 8. Answer = 899. 100 = 1; with borrow of 1 from next column 0 -1 (borrow) - 0 = 1, with borrow of (borrow) - 0 = 0. Answer = 011.

9 Signed Numbers Number systems Sign and magnitude
Ones-complement ( 9’s Complement ) Twos-complement (10’s Complement )

10 Arithmetic 6912 10101 Decimal Binary 11 1234 5274 + 5678 – 1638 3636
1011 + 1010 10101 1011 – 0110 0101

11 Negative numbers Sign and magnitude Ones-complement Twos-complement
How do we write negative binary numbers? Prefix numbers with minus symbol? 3 approaches: Sign and magnitude Ones-complement Twos-complement All 3 approaches represent positive numbers in the same way

12 Sign and magnitude Most significant bit (MSB) is the sign bit
0 ≡ positive 1 ≡ negative Remaining bits are the number's magnitude 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 0 – 1 – 2 – 3 – 4 – 5 – 6 – 7

13 Sign and magnitude Problem 1: Two representations of for zero
+0 = 0000 and also –0 = 1000 Problem 2: Arithmetic is cumbersome 4 – 3 != 4 + (-3)

14 Ones(1’s)-complement Negative number: Bitwise complement of positive number 0111 ≡ 710 1000 ≡ –710 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 7 – 6 – 5 – 4 – 3 – 2 – 1 – 0

15 1’s complement Solves the arithmetic problem end-around carry

16 Why 1’s complement works
The ones-complement of an 4-bit positive number y is – y 0111 ≡ 710 11112 – = ≡ –710 What is 11112? 1 less than = 24 – 1 –y is represented by (24 – 1) – y

17 So what's wrong? Still have two representations for zero!
+0 = 0000 and also –0 = 1111

18 Twos (2’s)-complement Negative number: Bitwise complement plus one
0111 ≡ 710 1001 ≡ –710 Benefits: Simplifies arithmetic Only one zero! 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 8 – 7 – 6 – 5 – 4 – 3 – 2 – 1

19 2’s complement

20 Obtaining 2’s complement
Recall: The ones-complement of a b-bit positive number y is (2b – 1) – y Twos-complement adds one to the bitwise complement, thus, –y is 2b – y Or Leaving all zeros and the first one from the right as it is then, complement each 0 and 1 after the first 1 from the right

21 Why 2’s complement works
Adding representations of x and –y where x, y are positive numbers, we get x + (2b – y) = 2b + (x – y) If there is a carry, that means that x  y and dropping the carry yields x – y If there is no carry, then x < y, then we can think of it as 2b – (y – x), which is the twos-complement representation of the negative number resulting from x – y.

22 Miscellaneous Sign-extension
Write +6 and –6 as twos-complement 0110 and 1010 Sign-extend to 8-bit bytes and Can't infer a representation from a number 11001 is 25 (unsigned) 11001 is -9 (sign and magnitude) 11001 is -6 (ones complement) 11001 is -7 (twos complement)

23 2's Complement Arithmetic
Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 2's Complement Arithmetic 10’s Complement Process The 10’s Complement process uses base-10 (decimal) numbers. Later, when we’re working with base-2 (binary) numbers, you will see that the 2’s Complement process works in the same way. First, complement all of the digits in a number. A digit’s complement is the number you add to the digit to make it equal to the largest digit in the base (i.e., 9 for decimal). The complement of 0 is 9, 1 is 8, 2 is 7, etc. Second, add 1. Without this step, our number system would have two zeroes (+0 & -0), which no number system has. This slide describes the 10’s complement conversion process. Project Lead The Way, Inc. Copyright 2009

24 10’s Complement Examples
2's Complement Arithmetic Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 10’s Complement Examples Example #1 -003 Complement Digits  996 Add 1 +1 997 Example #2 Examples of the 10’s Complement Process. -214 Complement Digits  785 Add 1 +1 24 786 Project Lead The Way, Inc. Copyright 2009 24

25 8-Bit Binary Number System
Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 2's Complement Arithmetic 8-Bit Binary Number System Apply what you have learned to the binary number systems. How do you represent negative numbers in this 8-bit binary system? Cut the number system in half. Use – to indicate positive numbers. Use – to indicate negative numbers. Notice that is not positive or negative. +127 pos(+) +126 +125 +1 -1 Introduction to the 8-Bit Binary Number system and how negative numbers are represented. -2 -127 neg(-) -128 Project Lead The Way, Inc. Copyright 2009

26 2's Complement Arithmetic
Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 2's Complement Arithmetic Sign Bit What did do you notice about the most significant bit of the binary numbers? The MSB is (0) for all positive numbers. The MSB is (1) for all negative numbers. The MSB is called the sign bit. In a signed number system, this allows you to instantly determine whether a number is positive or negative. +127 pos(+) +126 +125 +1 -1 Explanation of the sign bit. -2 -127 neg(-) -128 Project Lead The Way, Inc. Copyright 2009

27 2's Complement Arithmetic
Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 2's Complement Arithmetic 2’S Complement Process The steps in the 2’s Complement process are similar to the 10’s Complement process. However, you will now use the base two. First, complement all of the digits in a number. A digit’s complement is the number you add to the digit to make it equal to the largest digit in the base (i.e., 1 for binary). In binary language, the complement of 0 is 1, and the complement of 1 is 0. Second, add 1. Without this step, our number system would have two zeroes (+0 & -0), which no number system has. This slide describes the 2’s complement conversion process. Project Lead The Way, Inc. Copyright 2009

28 2’s Complement Examples
Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 2's Complement Arithmetic 2’s Complement Examples Example #1 5 = Complement Digits  Add 1 +1 -5 = Example #2 Examples of the 2’s Complement Process. -13 = Complement Digits  Add 1 +1 13 = Project Lead The Way, Inc. Copyright 2009

29 Using The 2’s Complement Process
Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 2's Complement Arithmetic Using The 2’s Complement Process Use the 2’s complement process to add together the following numbers. POS + POS  9 + 5 14 NEG + POS  (-9) + 5 - 4 This slide show that there are only four possible combinations for adding together two signed numbers. The next four slides demonstrate each of these examples. POS + NEG  9 + (-5) 4 NEG + NEG  (-9) + (-5) - 14 Project Lead The Way, Inc. Copyright 2009

30 2's Complement Arithmetic
Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 2's Complement Arithmetic POS + POS → POS Answer If no 2’s complement is needed, use regular binary addition. 9 + 5 14  +   Addition of two Positive numbers. Project Lead The Way, Inc. Copyright 2009

31 2's Complement Arithmetic
Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 2's Complement Arithmetic POS + NEG → POS Answer Take the 2’s complement of the negative number and use regular binary addition. 9 + (-5) 4  +  1] 8th Bit = 0: Answer is Positive Disregard 9th Bit This example shows the addition of one positive and one negative numbers. Note that this is done in the same way as subtracting a positive number from a positive number. In this case, the answer is positive.  +1 2’s Complement Process Project Lead The Way, Inc. Copyright 2009

32 2's Complement Arithmetic
Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 2's Complement Arithmetic POS + NEG → NEG Answer Take the 2’s complement of the negative number and use regular binary addition. (-9) + 5 -4  +  8th Bit = 1: Answer is Negative This slide demonstrates the addition of one positive and one negative number. Again, this is is the same a subtracting a positive number from a positive number. In this case the answer happens to be negative.  +1 To Check: Perform 2’s Complement On Answer  +1 2’s Complement Process Project Lead The Way, Inc. Copyright 2009

33 2's Complement Arithmetic
Digital Electronics  Lesson 2.4 – Specific Comb Circuit & Misc Topics 2's Complement Arithmetic NEG + NEG → NEG Answer Take the 2’s complement of both negative numbers and use regular binary addition. (-9) + (-5) -14  2’s Complement Numbers, See Conversion Process In Previous Slides  +  1] 8th Bit = 1: Answer is Negative Disregard 9th Bit This slide demonstrates the addition of two negative numbers.  +1 To Check: Perform 2’s Complement On Answer Project Lead The Way, Inc. Copyright 2009

34 Overflow Summing two positive numbers can give a negative result
Summing two negative numbers can give a positive result 0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 8 – 7 – 6 – 5 – 4 – 3 – 2 – 1 6 + 4 ⇒ –6 –7 – 3 ⇒ +6

35 Addition Examples (8 bit numbers)
Add 7 and 4 (both positive) Add 15 and -6 (positive > negative) Add 16 and -24 (negative > positive) Add -5 and -9 (both negative) (-6) Discard carry (-24) Sign bit is negative so negative number in 2’s complement form Discard carry

36 Subtraction Examples Find 8 minus 3. Find 12 minus -9.
Discard carry Minuend Subtrahend Difference Discard carry

37 BCD Addition

38 BCD Addition Rules Result must be corrected if
1. Result > 9 (further during all the addition steps) 2. A carry occurs in the first addition step only from one digit ( 4bit binary ) to another.

39 BCD example 0101 0010 0100 0110 1001 1000 nc Example #1 52 + 46
98 No correction is needed 1001 1000 nc

40 BCD example Example #2 + nc nc Correction

41 BCD example Example #3 + nc c Correction Copyright Joanne DeGroat, ECE, OSU 9/15/09 - L2

42 BCD example Example #4 + nc nc Correction

43 BCD example Example #5 + nc nc Correction

44


Download ppt "Computer Engineering (Logic Circuits) Dr. Tamer Samy Gaafar Lec. # 2"

Similar presentations


Ads by Google