Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.

Slides:



Advertisements
Similar presentations
COE 202: Digital Logic Design Signed Numbers
Advertisements

King Fahd University of Petroleum and Minerals
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Number Systems Standard positional representation of numbers:
Assembly Language and Computer Architecture Using C++ and Java
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
VIT UNIVERSITY1 ECE 103 DIGITAL LOGIC DESIGN CHAPTER I NUMBER SYSTEMS AND CODES Reference: M. Morris Mano & Michael D. Ciletti, "Digital Design", Fourth.
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.
Number Systems Lecture 02.
Chapter 3 Data Representation part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Binary Representation and Computer Arithmetic
Digital Systems and Binary Numbers
Computer Arithmetic Nizamettin AYDIN
Representing Integer Data Book : Chapter ( Subject has no point !! ) A99ACF.
IT253: Computer Organization
Eng. Mohammed Timraz Electronics & Communication Engineer University of Palestine Faculty of Engineering and Urban planning Software Engineering Department.
Lecture Overview Introduction Positional Numbering System
Chapter 4 – Arithmetic Functions and HDLs Logic and Computer Design Fundamentals.
Number Systems. Why binary numbers? Digital systems process information in binary form. That is using 0s and 1s (LOW and HIGH, 0v and 5v). Digital designer.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
ECE 301 – Digital Electronics Unsigned and Signed Numbers, Binary Arithmetic of Signed Numbers, and Binary Codes (Lecture #2)
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
CSC 221 Computer Organization and Assembly Language
Charles Kime & Thomas Kaminski © 2004 Pearson Education, Inc. Terms of Use (Hyperlinks are active in View Show mode) Terms of Use Logic and Computer Design.
ECE 2110: Introduction to Digital Systems Signed Addition/Subtraction.
Operations on Bits Arithmetic Operations Logic Operations
Computer Arithmetic See Stallings Chapter 9 Sep 10, 2009
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
CEC 220 Digital Circuit Design Binary Arithmetic & Negative Numbers Monday, January 13 CEC 220 Digital Circuit Design Slide 1 of 14.
Representing Integer Data
Chapter 1: Binary Systems
IT1004: Data Representation and Organization Negative number representation.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic Herbert G. Mayer, PSU Status 1/14/2016 Copied with Permission from prof. Mark PSU ECE.
Two’s and one’s complement arithmetic CLOCK ARITHMETIC.
ECE DIGITAL LOGIC LECTURE 3: DIGITAL COMPUTER AND NUMBER SYSTEMS Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2016, 01/19/2016.
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
DATA REPRESENTATION Data Types Complements Fixed Point Representations
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Lecture 4: Digital Systems & Binary Numbers (4)
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
CS2100 Computer Organisation
Cosc 2150: Computer Organization
Backgrounder: Binary Math
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
CHAPTER 4: Representing Integer Data
CS1010 Programming Methodology
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
Data Representation Data Types Complements Fixed Point Representation
Data Representation in Computer Systems
Digital Logic & Design Lecture 02.
EE207: Digital Systems I, Semester I 2003/2004
ECEG-3202 Computer Architecture and Organization
Data Representation – Chapter 3
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic
ECE 331 – Digital System Design
Chapter3 Fixed Point Representation
Presentation transcript:

Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department

The Hashemite University2 Outline Introduction. Numbers range. Sign extension. Overflow detection. Unsigned numbers addition and subtraction. Signed numbers addition and subtraction. Examples.

The Hashemite University3 Introduction Computers perform the arithmetic operations in Binary. Computers perform arithmetic on fixed-size numbers which is called finite-precision arithmetic that is the operands and the result are stored in a fixed size memory storage and cannot be exceeded. The rules for finite-precision arithmetic are different from the rules of ordinary arithmetic (which are performed by humans in decimal). The general design rule is to simplify operations as possible in order to get simpler and lower cost logic circuits implementation for these operations.

The Hashemite University4 Numbers in Computers Two types of numbers are found in computers: Unsigned: the concept of +ve and –ve numbers does not exist. Simply can be considered to contain +ve numbers only where all bits within the number can be used to represent the magnitude of the number (no bits are reserved). Signed: the number contains a sign representation. So, you have +ve and –ve numbers. One bit (which is the MSB) is reserved for the sign. So, the magnitude of the number occupies (n – 1) bits (for an n bit number). Unsigned numbers have exactly one representation in Binary systems. Signed numbers have many different representation systems where we will deal with only 3 of them. Signed and unsigned concepts are applicable for all numbering systems (decimal, octal, hexa, binary, …).

The Hashemite University5 Signed Binary Numbers In decimal system the sign is represented using – and + symbols. However, such symbols cannot be used by hardware where every thing must be binary (either 0 or 1). So, the MSB of a binary number is reserved for the sign (i.e. sign bit): 0  +ve number. 1  -ve number.

The Hashemite University6 Signed Binary Numbers Representation Signed-magnitude system: the MSB represents the sign, the remaining bits represent the number value (the value is the same as in unsigned numbers). Negates the number by reversing its sign. Hard to be used for binary operations in computers. Signed-complement system: the MSB gives an indication about the sign but not reserved for it. Negates a number by taking its complement (either 1’s or 2’s complement). Taking the complement of the positive number includes the sign bit which becomes 1 (-ve) in all cases. Convenient to be used by computers (actually the 2’s complement is the most common).

The Hashemite University7 Complements The complement of a number B is the number that when added to B you get either the quantity r n or (r n – 1) based on the complement type. Two types of complement systems: Diminished radix complement: or simply (r – 1)’s complement (r is the radix). Radix complements: or simply r’s complement. In binary they are called: 1’s and 2’s complement. In decimal: 9’s and 10’s complement. In octal: 6’s and 7’s complement. In hexa: 15’s and 16’s complement. We are mainly interested in binary and decimal complements (to see how arithmetic operations are performed by computers).

The Hashemite University8 Diminished Radix Complement In this system we have: B + (-B) = (r n - 1) So, in general for number N which contains n digits: (r – 1)’s complement = (r n - 1) – N Very Important Note: r n is computed in decimal then before subtraction you convert it into the proper numbering system that you are working with. OR just put a 1 followed by n zeros.

The Hashemite University9 Binary 1’s Complement – Simple Rule Simply invert each bit in the binary number: 1  0 and 0  1 Examples: Number1’s complement

The Hashemite University10 Floating Point Diminished Radix Complement Remove the radix point temporarily and find the (r – 1)’s complement then put the floating point in its original relative location in the result (the same location found in the original number).

The Hashemite University11 Floating Point Diminished Radix Complement -- Examples Number(r – 1)’s Complement  ’s Complement =  (final result)  ’s Complement =  (final result)

The Hashemite University12 Radix Complement In this system we have: B + (-B) = r n So, for number N which contains n digits: r’s complement = r n – N So: r’s complement = 1 + (r - 1)’s complement The rule of r n calculations is the same as in the (r-1)’s complement. Special rule for 2’s complement in Binary: Start from the LSB and leave all 0’s without change in addition to the first 1 you encounter. After that invert all remaining bits (0  1 and 1  0). For floating point apply the same rule used by (r – 1)’s complement: Remove the radix point temporarily and find the r’s complement then put the floating point in its original relative location in the result.

The Hashemite University13 Radix Complement -- Examples Numberr’s Complement  ’s Complement =  (final result)

The Hashemite University14 Remember... Negative of the negative is positive. So, applying complement twice for a number you return to the original number (none complemented one).

The Hashemite University15 Number Ranges I Unsigned System: Has one zero Number range: 0 to 2 n -1 Signed-magnitude and the signed 1’s complement: Have two zeros (-ve and +ve zero). Symmetric values for both +ve and –ve directions (i.e. mirror). Number range: -(2 n – 1 - 1) to +(2 n – 1 - 1). Signed 2’s complement: Has one +ve zero (more natural). Asymmetric (there is an additional value in the negative direction). Number range: - 2 n – 1 to +(2 n – 1 - 1).

The Hashemite University16 Number Ranges II Note that: The number of values that can be represented by an n bit number = 2 n (same for signed and unsigned numbers)

The Hashemite University17 Number Format and Arithmetic Operations In life we are working with: Integer numbers: no fractions. Real numbers: a floating point exists. We need special representation for floating point numbers since computers deals with fixed size numbers represented in binary. Many standards exist to represent real numbers in binary, e.g. NIST, IEEE 754 standard. Arithmetic operations: Addition. Subtraction. Multiplication. Division. In this lecture we will deal with addition and subtraction (only) for signed and unsigned Integer numbers and floating point numbers in ordinary representation (contain radix point as we write in daily life).

The Hashemite University18 Arithmetic Operations -- Notes Big Picture: 1.Make sure that the two number you are adding os subtracting are of the same size (have the same number of bits). This is done by sign extension as shown in the next slide. 2.Perform the required operation (addition or subtraction). 3.Check if there is overflow or not (see next slides). If there is overflow, then the operation is incorrcet, no answer. If there is not overflow, then the operation is correct, take the answer.

The Hashemite University19 Sign Extension Concept I If you are given the following two unsigned numbers to be added which they are not of equal number of bits what will you do? Sol: Remember in the unsigned system there is no –ve numbers. So, to reserve the value of the number you must add zeros to the left of the number (since they have no value) So it becomes 

The Hashemite University20 Sign Extension Concept II If you are given the following two signed numbers (using the 2’s complement) to be added which they are not of equal number of bits what will you do? Sol: is a –ve number since MSB = 1. you must extend the sign bit to reserve the sign of the number. So it becomes 

The Hashemite University21 Overflow Arithmetic operation may not obtain the correct answer in some cases. This happens when the result is out of range that the reserved bits can represent. For example, when 8-bit binary code is used to represent a number in the 2’s complement system, the range of numbers that can be represented is from -128 to If the result of an operation lies outside this range, overflow occurs. Overflow occurs because of the finite precision arithmetic of the computer hardware. Computers checks for overflow and set a flag in case of over flow. The user can check this flag to know the current overflow status.

The Hashemite University22 Overflow Detection Overflow cases: Unsigned numbers: Addition: an overflow occurs if C out from the MSB is not equal to 0. Subtraction: as in signed numbers as shown below. For signed numbers three cases exist this depends on the used system to represent –ve numbers: Sign-magnitude system  will not be studied 1’s and 2’s complement system  C in != C out means there is an overflow (C in is the carry into the MSB). This is checked in the 1’s complement before performing the end-around carry step.

The Hashemite University23 Binary Addition and subtraction We will study them as follows: Unsigned Addition and subtraction: deal with unsigned number systems. Signed Addition and subtraction: deal with signed number systems both 1’s and 2’s complement systems.

The Hashemite University24 Binary Addition – Basic Carry Bit (a) (b) (c)(d) (e) Carry Bit Remember Counting Principles

The Hashemite University25 Unsigned Binary Addition -- Examples (a)(b)(c) (d) (e) All calculations are right, no overflow, (Cout = 0)!!

The Hashemite University26 Unsigned Addition – Other Systems Pay attention to the used numbering system. Simply convert the number into decimal or binary (if you are working in octal or hexa) and perform addition then convert the result back to the original numbering system that you started from.

The Hashemite University27 Unsigned Binary Subtraction I All subtraction operations can be converted into addition by taking complements (to reserve the simplicity rule): A – B = A + (-B) Remember we are dealing with unsigned numbers (no –ve numbers). So, (-B) in the above example is the complement of B not the –ve quantity of B. Such general rule is applied for all numbering systems.

The Hashemite University28 Unsigned Binary Subtraction II So, perform unsigned subtraction using complements: r’s complement unsigned subtraction (2’s complement in binary). (r – 1)’s complement unsigned subtraction (1’s complement in binary). Different rules are applied for each method. The basic method is the r’s complement unsigned subtraction.

The Hashemite University29 r’s Complement Unsigned Subtraction To subtract M – N: Find the r’s complement of N and add it to M. If a carry out produced then it must be discarded which means that M >= N. If no carry out produced this means that M < N, in this case the result of the subtraction is – (N – M), i.e. –ve of the difference. So, find the r’s complement of the result and place a – sign in front of it.

The Hashemite University30 Examples I Ex. 1: Compute – using 2’s complement, n = = (25 10 ) = ( ) = (0 10 ) discard The calculation is right, no overflow, (Cin = Cout)!!

The Hashemite University31 Examples II Ex: Compute – using 2’s complement, n = = ( ) = (2’s) = ( ) = ( ) = (76 10 ) discard The calculation is right, no overflow, (Cin = Cout)!!

The Hashemite University32 Examples III Ex. 3: Compute – using 2’s complement, n = 4. – = (2’s) The calculation is wrong !! Cin != Cout, Overflow

The Hashemite University 33 Examples IV Ex. 4: Compute – using 2’s complement, n = 4. – = (2’s) => no carry out The answer is the 2’s complement of 1011= - (0101) The calculation is right, no overflow, (Cin = Cout)!!

The Hashemite University34 (r – 1)’s Complement Unsigned Subtraction To subtract M – N: Find the (r – 1)’s complement of N and add it to M. If a carry out produced then it must be discarded (which means that M >= N) and then add 1 to the result. This method is called end-around carry. If no carry out produced this means that M < N, in this case the result of the subtraction is – (N – M), i.e. –ve of the difference. So, find the (r - 1)’s complement of the result and place a – sign in front of it.

The Hashemite University35 Examples I Ex. 1: Compute – using 1’s complement, n = = (25 10 ) = ( ) Final answer = - (1’s of ) = The calculation is right, no overflow, (Cin = Cout)!!

The Hashemite University36 Examples II Ex. 2: Compute – using 1’s complement, n = = ( ) = (1’s) = ( ) = ( ) = (76 10 ) discard The calculation is right, no overflow, (Cin = Cout)!!

The Hashemite University37 Examples III Ex. 3: Compute – using 1’s complement, n = 4. – = (1’s)  No carry out The calculation is wrong !! Cin != Cout, Overflow

The Hashemite University38 Example IV Ex. 4: Compute 11 2 – using 2’s complement where n = 8. – = (2’s) discard The calculation is right, no overflow, (Cin = Cout)!!

The Hashemite University 39 Examples V Ex. 5: Compute – using 1’s complement, n = 4. – = (1’s) => no carry out The answer is the 1’s complement of 1011= - (0100) The calculation is right, no overflow, (Cin = Cout)!!

The Hashemite University40 Signed Binary Arithmetic Which system to use? We will use the complement signed system. Using signed complements we need one circuit to perform both addition and subtraction because subtraction can be done by addition. No sign checking of operands is required  simpler in hardware. Also, overflow detection is very easy using this system. 2’s complement is preferred over 1’s complement because no duplication of zero exist.

The Hashemite University41 2’s Complement Signed Binary Addition Represent –ve numbers in 2’s complement (if not already in binary). Add numbers and if a carry out obtained discard it. If the result is –ve (i.e. the MSB is 1) then it is in the 2’s complement form.

The Hashemite University42 1’s Complement Signed Binary Addition Represent –ve numbers in 1’s complement (if not already in binary). Add numbers and if a carry out obtained discard it and add 1 to the result (end-around carry method). If the result is –ve (i.e. the MSB is 1) then it is in the 1’s complement form.

The Hashemite University43 Signed Binary Subtraction Convert the subtraction into addition by taking the complement of the second operand. Then, apply the same rules of addition for both types of complements. Note that arithmetic operations for both signed and unsigned numbers are the same but the difference lies in the interpretation of the results.

The Hashemite University44 Example I Find the following using 2’s complement (signed): = (-39) First: convert into binary 115 (10) = (2) (2‘s) -39 (10) = (2) (2’s) Second: perform subtraction using 2’s complement: = (115) = (-39) = (76) discard The calculation is right, no overflow, (Cin = Cout)!!

The Hashemite University45 Example II Find the following using 2’s complement (signed): First: convert into binary: 68 (10) = (2) (2‘s) 99 (10) = (2) (2’s) Second: add using 2’s complement: = (68) = (99) = (-89) The calculation is wrong !! Cin != Cout The actual answer (i.e. 167) exceeds the largest limit (i.e. 127) =>OVERFLOW

The Hashemite University46 Example III Find the following using 2’s complement (signed) : = (-78) + (-52) First: convert into binary: -78 (10) = (2) (2‘s) -52 (10) = (2) (2’s) Second: subtract using 2’s complement : = (-78) = (-52) = (126) The calculation is wrong !! Cin != Cout overflow discard

The Hashemite University 47 Examples IV Ex. : Compute – using 1’s complement, n = 4, signed. – = (1’s) => no carry out The answer is 1011 The calculation is right, no overflow, (Cin = Cout)!!

The Hashemite University 48 Examples V Ex. 5: Compute – using 2’s complement, n = 4, signed. – = (2’s) => no carry out The answer is 1011 The calculation is right, no overflow, (Cin = Cout)!!

The Hashemite University49 Additional Notes This lecture covers the following material from the textbook: Chapter 1: Sections 1.5 – 1.6