Binary Addition & Subtraction. Unsigned and Sign & Magnitude numbers

Similar documents
unused unused unused unused unused unused

Module 2: Computer Arithmetic

Binary Addition. Add the binary numbers and and show the equivalent decimal addition.

Signed umbers. Sign/Magnitude otation

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

CHW 261: Logic Design

Chapter 2. Data Representation in Computer Systems

CO212 Lecture 10: Arithmetic & Logical Unit

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

D I G I T A L C I R C U I T S E E

CS 101: Computer Programming and Utilization

Lecture (02) Operations on numbering systems

BINARY SYSTEM. Binary system is used in digital systems because it is:

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

Signed Binary Numbers

Chapter 4. Operations on Data

CHAPTER 5: Representing Numerical Data

Digital Fundamentals

Number System. Introduction. Decimal Numbers

MC1601 Computer Organization

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Number Systems. Both numbers are positive

Chapter 1 Review of Number Systems

Divide: Paper & Pencil

COMPUTER ORGANIZATION AND ARCHITECTURE

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Computer Organization

Introduction to Computer Science-103. Midterm

UNIT - I: COMPUTER ARITHMETIC, REGISTER TRANSFER LANGUAGE & MICROOPERATIONS

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

FLOATING POINT NUMBERS

Chapter 5 : Computer Arithmetic

Topic Notes: Bits and Bytes and Numbers

Internal Data Representation

Principles of Computer Architecture. Chapter 3: Arithmetic

A Level Computing. Contents. For the Exam:

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

Chapter 3: Arithmetic for Computers

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

MACHINE LEVEL REPRESENTATION OF DATA

(+A) + ( B) + (A B) (B A) + (A B) ( A) + (+ B) (A B) + (B A) + (A B) (+ A) (+ B) + (A - B) (B A) + (A B) ( A) ( B) (A B) + (B A) + (A B)

COMPUTER ARITHMETIC (Part 1)

COMP Overview of Tutorial #2

Introduction to Computers and Programming. Numeric Values

Chapter 2: Number Systems

CHAPTER TWO. Data Representation ( M.MORRIS MANO COMPUTER SYSTEM ARCHITECTURE THIRD EDITION ) IN THIS CHAPTER

Numerical Representations On The Computer: Negative And Rational Numbers

EE 109 Unit 6 Binary Arithmetic

Numerical Representations On The Computer: Negative And Rational Numbers

ECE/Comp Sci 352 Digital Systems Fundamentals. Charles R. Kime Section 2 Fall Logic and Computer Design Fundamentals

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

Chapter 3: part 3 Binary Subtraction

±M R ±E, S M CHARACTERISTIC MANTISSA 1 k j

CS101 Lecture 04: Binary Arithmetic

CMPSCI 145 MIDTERM #1 Solution Key. SPRING 2017 March 3, 2017 Professor William T. Verts

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

MYcsvtu Notes DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes

Topic Notes: Bits and Bytes and Numbers

Chapter 10 - Computer Arithmetic

Arithmetic Operations on Binary Numbers

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

Chapter 2. Data Representation in Computer Systems

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Floating Point Arithmetic

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Digital Fundamentals

Organisasi Sistem Komputer

Numbering systems. Dr Abu Arqoub

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Chapter 1. Digital Systems and Binary Numbers

CSE 140 Homework One

Computer Sc. & IT. Digital Logic. Computer Sciencee & Information Technology. 20 Rank under AIR 100. Postal Correspondence

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

Number Systems and Computer Arithmetic

UNIT 7A Data Representation: Numbers and Text. Digital Data

CHAPTER 2 Data Representation in Computer Systems

CHAPTER 2 Data Representation in Computer Systems

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Inf2C - Computer Systems Lecture 2 Data Representation

UNIVERSITY OF MASSACHUSETTS Dept. of Electrical & Computer Engineering. Digital Computer Arithmetic ECE 666

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

The type of all data used in a C (or C++) program must be specified

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

QUIZ ch.1. 1 st generation 2 nd generation 3 rd generation 4 th generation 5 th generation Rock s Law Moore s Law

Chapter 2 Data Representations

UNIT-III COMPUTER ARTHIMETIC

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Chapter 2. Data Representation in Computer Systems. Objectives (1 of 2) Objectives (2 of 2)

Advanced Computer Architecture-CS501

3.5 Floating Point: Overview

CHAPTER 2 (b) : AND CODES

Floating Point Numbers. Lecture 9 CAP

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

Representing Information. Bit Juggling. - Representing information using bits - Number representations. - Some other bits - Chapters 1 and 2.3,2.

Transcription:

Binary Addition & Subtraction Unsigned and Sign & Magnitude numbers Addition and subtraction of unsigned or sign & magnitude binary numbers by hand proceeds exactly as with decimal numbers. (In fact this is true for any radix numbers) Examples - Addition (unsigned) 0110010 2 31 5 A3D 16 + 1100110 2 + 14 5 + F2 16 10011000 2 100 5 B2F 16 If the result has more digits than can fit in the assigned storage word (i.e, there is a carry out of the last bit that can be held), then an overflow has occurred. Usually a computer error handler (program) is invoked to deal with the problem. Examples - Subtraction (unsigned) 0110010 2 14 5 A3D 16-0010110 2-31 5 - F2 16 0011100 2-12 5 94B 16 As in decimal, if the numbers are of different signs: 1. when adding you subtract the smaller from the larger and assign the sign of the larger number; 2. when subtracting, you must keep track of whether the negative number is the subtrahend (in which case you change its sign and add, getting a positive result) or the minuend (in which case you add without changing any signs, and the result is negative.) NTC Oct 17, 2005 Page 1 of 12

Practice Problems - Unsigned Arithmetic 1. Do the following binary additions a. 1001101 b. 11111 c. 101010 + 1011011 + 00001 +010101 --------------- ------------- -------------- 2. Do the following binary subtractions a. 100000 b. 110010 c. 1111111-000001 - 010001-1010101 -------------- -------------- ----------------- 2's Complement Arithmetic When the numbers are in 2's complement form, there is no need to keep track of whether the numbers have different signs and, if so, which is bigger. In the case of subtraction, however, the subtrahend must have its sign reversed; the numbers are then simply added normally. This sign reversal is accomplished by the complement and add procedure we ve already discussed. Examples - Addition (2's complement in 5-bit registers) 36. 01001 37. 01001 38. 01001 39. 10101 + 00011 + 01001 + 11111 + 11010 01100 10010 1 01000 1 01111 In 36, both numbers are positive and the expected result is achieved (in decimal, 9 + 3 = 12). In 37, both numbers are again positive, but the result is negative (note the leading 1)! It appears that 9 + 9 = - 14. This is considered an overflow situation, even though no carry actually overflowed out of the most significant bit. In a computer, hardware is implemented to detect such overflows, and they do so as follows. Consider the two most significant bit positions, b n-1 and b n-2. Call the carries out of these c n-1, c n-2, where c n-1 is the bit that actually overflows into the next (non-existent) bit b n. Then an overflow is detected whenever c n-1 <> c n-2. In example b), c n-1 = 0 (there is no carry) and c n-2 is 1 (there is a carry); Since they are different, an overflow is detected, NTC Oct 17, 2005 Page 2 of 12

and the answer is known to be incorrect. In 38, one number is positive and the other is negative. There appears to be an overflow, but in fact the answer (ignoring c n-1 ) is correct (9 + (- 1) = 8). Notice that the conditions for detecting an actual overflow as described above do not exist: both c n-1 (c 5 )and c n-2 (c 4 ) are 1; since they are the same, there is no overflow detected, and the answer, ignoring c 5, is correct. In 39, both numbers are negative. The answer appears to be +15, an unlikely result when adding two negative numbers. However, since c 4 and c 3 are different (c 3 =0, c 4 =1), we know that an overflow has occurred, and therefore the answer is incorrect. Examples - subtraction (2's complement in 5-bit registers) 40. 01001 41. 11001 42. 01001 43. 10101-00011 - 01010-11111 - 11010 In each of the above cases, the subtrahend must be converted to its 2's complement value and then normal addition is done. 01001 11001 01001 10101 + 11101 + 10110 + 00001 + 00110 1 00110 1 01111 01010 11011 Checking for overflows in each of these examples shows one only in example 41. We can conclude, then, that all the results except41 are correct. NTC Oct 17, 2005 Page 3 of 12

Practice Problems - 2's Complement Arithmetic 1. Do the following 2's complement additions. In all cases assume results are placed in an 8-bit register. For each problem, indicate whether the addition resulted in an overflow. a. 01100001 b. 01100001 c. 01101111 d. 11110000 00001111 00101110 11111111 00010000 ------------- ------------- -------------- ------------- 2. Do the following 2's complement subtractions. In all cases assume results are placed in an 8-bit register. For each problem, indicate whether the subtraction resulted in an overflow. a. 01010100 b. 00000110 c. 10000000 d. 11100011-00101001 - 10000011-00000001 - 11100011 ----------------- ----------------- ----------------- ----------------- BCD Arithmetic Consider the addition of the two unsigned BCD numbers 29 and 37. 44. 0010 1001 + 0011 0111 0110 0000 The answer shown, 60, is clearly incorrect. It looks like it might be correct, since both digits are less than 9. Consider another example, 16 and 69. 45. 0001 0110 + 0110 1001 0111 1111 This answer is clearly wrong since one of the digits is not a legal decimal digit. You might ask, why are we doing binary arithmetic on BCD numbers? Shouldn t we be building hardware to do BCD arithmetic. The answer is that, yes we could build hardware to do this (some IBM System/360 models did this), but it is very wasteful to design and build special (BCD) hardware that duplicates the function of other (binary) hardware, if the binary hardware can be modified inexpensively to do the same job. In the case of BCD arithmetic, this is easy to do, and it is done using something called NTC Oct 17, 2005 Page 4 of 12

excess-3 coding (or excess-6 coding, or both). Excess-3 coding simply recodes every digit to be used in the arithmetic calculation by adding 3 to it, as in Table TN5 Decimal BCD Excess-3 0 1 2 3 4 5 6 7 8 9 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 Table TN5. Excess-3 Coding Now consider example 44. Rewriting in excess-3 coding results in 0010 1001 (29) + 0011 0111 (37) 0101 1100 + 0110 1010 giving an answer of 1100 0110 (C6) This still isn t correct, but notice: the low order nybble does have the correct answer (6), and the high order nybble differs from the correct answer by 6. In fact, we should expect both digits to be 6 more than they should be since we added 3 to all of the digits before we started. As we can see, we need to subtract 6 from the first digit and leave the second digit alone. The reason is, again, involved with carries, or overflows, out of the digits. The general rule is, 1. if the digit has a carry out of it, it will have the correct answer; 2. if there is no carry out of a digit, then we must subtract six from the digit to correct it. NTC Oct 17, 2005 Page 5 of 12

Applying this principle to the current example, the first digit had no carry out, while the second digit did. The adjusted result, after subtracting six from the first digit, is 0110 0110 (66). Let s apply these rules to example 45.. Adding 3 to every digit gives 0001 0110 (16) + 0110 1001 (69) 0100 1001 + 1001 1100 which, adding, gives 1110 0101 The carries in this case happen to be the same as in the previous example, so we need to subtract 6 from the first digit, giving the result 1000 0101 (85), the correct answer. Now consider the subtraction of two BCD numbers. We will assume unsigned binary subtraction algorithms, rather than 9's complement or 10's complement numbers, as is sometimes done. Let s subtract 23 from 85 1000 0101 (85) - 0010 0011 (-23) We again convert these numbers into excess-3 notation: 1011 1000-0101 0110 Applying the rules of unsigned binary subtraction gives a result of 0110 0010 (62) which is the correct answer, without any adjustment being necessary! We should expect this to occur, since subtracting removes all the excess-3 coding automatically. In this case, (5+3) - (3+3) = 5+3-3-3 = 5-3 and (8+3) - (2+3) = 8+3-2-3 = 8-2. Of course, as you might expect, we were just lucky. Let s take another example, say 81-23. 1000 0001 becomes 1011 0100 0010 0011 0101 0110 and subtracting gives 0101 1110. If we subtract 6 from the second digit, the answer is now correct: 0101 1000. Thus, a similar rule applies for subtraction as we used for addition, only now instead of looking for a carry out of a digit, we are looking for a borrow into a digit. If such a borrow exists, then subtracting 6 will correct the digit. NTC Oct 17, 2005 Page 6 of 12

It is important to note that even though the answer may consist of all legal digits, adjustments may still be necessary. One last example addition example 46. 0001 0000 (10) ==excess-3==> 0100 0011 + 0010 0011 (23) 0101 0110 -------------- 1001 1001 Although the answer, 99, contains valid decimal digits, it is incorrect. The correct answer is 33, and we know this because neither digit had a carry out, requiring that we subtract 6 from each of them. Practice Problems - BCD Arithmetic 1. Do the following BCD additions. For each problem, state which digit(s), if any, required adjustment. a. 00100101 b. 00010000 c. 01010111 + 01000101 + 00100110 + 10000101 ----------------- ----------------- ------------------ 2. Do the following BCD subtractions. For each problem, state which digit(s), if any, required adjustment. a. 10010010 b. 00111000 d. 10011001-00010011 - 00000110-01100100 ---------------- ------------------ ------------------ Floating point arithmetic Addition and subtraction of floating point numbers is the same as adding and subtraction sign & magnitude numbers, with one little adjustment: aligning the decimal points. Consider the addition of two radix-10 floating point numbers 35.71732 x 10 3 + 451.213 x 10-1 NTC Oct 17, 2005 Page 7 of 12

Just for the sake of discussion let s look at these numbers in their fixed-point form: 35717.32 45.1213 Notice that we ve lined up the decimal points, a necessity in fixed-point addition. It is also a necessity in floating-point arithmetic, and consists of adjusting one or the other of the exponents so that the two exponents are the same. Of course, whichever number has its exponent adjusted will also have the decimal point moved an appropriate amount. Which number to adjust? We generally choose to increase the size of the smaller exponent. This is because increasing the exponent of a number will require moving the decimal point to the left. In hardware, shifting a radix point to the left is accomplished by shifting the entire number to the right in its register; if any digits are lost in this shifting process they are the least significant digits at the rightmost end of the register. (Note that sometimes it is necessary to shift both numbers. Remember that in any practical implementation of floating point numbers there is a limit on the maximum and minimum values that the exponent may have. It may be necessary to adjust both exponents to ensure that they are both within the allowed range.) In our current example, these guidelines suggest that the second number be adjusted by adding 4 to its exponent and shifting the decimal point left four places, giving 35.71732 x 10 3 +.0451213 x 10 3 The numbers can now be added, giving 35.7634413 x 10 3. While this is a correct answer, it is usual to normalize the result, giving a final answer of 3.57634413 x 10 4 We can now formulate the procedure for floating point arithmetic which we can use for binary numbers as well as for decimal numbers. We will assume that floating point numbers to be added or subtracted are already in IEEE format. 1. Subtract the exponents to determine which is smaller and determine the difference. 2. Add the difference to the smaller exponent. NTC Oct 17, 2005 Page 8 of 12

Notice that since we are using biased format for the exponents we need to nothing special to determine the sign of the result. However, we do need to ensure that the resultant exponent is less than or equal to the maximum attainable. (E.g. in IEEE single precision the maximum binary value is 11111111 (= 255 biased = +128 10 ) 3. Shift the mantissa to the right a number of places equal to the difference determined in 1. Don t forget to add the implied 1' in after the number has been shifted one place. 4. Add or subtract the mantissas as required by your original problem, taking into account the signs of the operands and adjusting the sign of the result, just as you would in decimal arithmetic. 5. The result of 4. may have 0, 1, or 2 significant digits to the left of the radix point (neither more not less), so it will have to be normalized, if necessary. Example 47: Add the two floating point numbers 3E340000 and 40FC0000 3E340000 = 0 01111100 011010... = + 1.01101 x 2 124 (exponent is excess 127) 40FC0000 = 0 10000001 11111000... = + 1.1111100 x 2 129 1. The first number has the smaller exponent, and the difference between the exponents is 5. 2. & 3. The first number s exponent becomes 124+5 = 129 and the mantissa is shifted right to produce.0000101101000... 4. Adding.000010110100... x 2 129 + 1.11111000... x 2 129 = 10.000000110100... x 2 129 5. Normalizing gives a final answer of 1.0000000110100... x 2 130 Notice that, since one of the numbers in step 4. will always be in the original normalized format, step 5., if required, will always involve a shift right or left (of at most one bit position) and a corresponding increase or decrease in the exponent by at most 1. Restoring the result to IEEE notation and then to hexadecimal shorthand gives NTC Oct 17, 2005 Page 9 of 12

0 10000010 00000001101000000000000 = 4100D000 16 Practice problems - Binary Floating Point Arithmetic 1. Add the following Binary floating point numbers. Normalize the result. 11100.1000100111100... x 2 100 and.00110111000... x 2 106 2. Add the following IEEE floating point numbers. Express the result in IEEE hexadecimal format 659B8000 and E5310000 3. Show the hexadecimal IEEE format for the values zero and one. NTC Oct 17, 2005 Page 10 of 12

Answers to Practice Problems Practice Problems - Unsigned Arithmetic 1. Do the following binary additions a. 1001101 b. 11111 c. 101010 + 1011011 + 00001 +010101 --------------- ------------- -------------- Ans. 10101000 100000 111111 2. Do the following binary subtractions a. 100000 b. 110010 c. 1111111-000001 - 010001-1010101 -------------- -------------- ----------------- Ans.: 011111 100001 0101010 Practice Problems - 2's Complement Arithmetic 1. Do the following 2's complement additions. In all cases assume results are placed in an 8-bit register. For each problem, indicate whether the addition resulted in an overflow. a. 01100001 b. 01100001 c. 01101111 d. 11110000 00001111 00101110 11111111 00010000 ------------- ------------- -------------- ------------- Ans.: 01110000 10001111 01101110 00000000 No Overflow Overflow No Overflow No Overflow 2. Do the following 2's complement subtractions. In all cases assume results are placed in an 8-bit register. For each problem, indicate whether the subtraction resulted in an overflow. a. 01010100 b. 00000110 c. 10000000 d. 11100011-00101001 - 10000011-00000001 - 11100011 ----------------- ----------------- ----------------- ----------------- Ans.: 00101011 10000011 01111111 00000000 No Overflow Overflow Overflow No Overflow Practice Problems - BCD Arithmetic NTC Oct 17, 2005 Page 11 of 12

1. Do the following BCD additions. For each problem, state which digit(s), if any, required adjustment. a. 00100101 b. 00010000 c. 01010111 + 01000101 + 00100110 + 10000101 ----------------- ----------------- ------------------ Ans.: 01110000 00110110 000101000010 Subtracted: 01100000 01100110 000000000000 2. Do the following BCD subtractions. For each problem, state which digit(s), if any, required adjustment. a. 10010010 b. 00111000 d. 10011001-00010011 - 00000110-01100100 ---------------- ------------------ ------------------ Ans.: 01111001 00110010 00110101 Subtracted: 00000110 00000000 00000000 Practice problems - Binary Floating Point Arithmetic 1. Add the following Binary floating point numbers. Normalize the result. 11100.1000100111100... x 2 100 and.00110111000... x 2 106 Ans.: 1.0101001001001111 x 2 105 2. Add the following IEEE floating point numbers. Express the result in IEEE hexadecimal format 659B8000 and E5310000 = 65060000 3. Show the hexadecimal IEEE format for the values zero and one. Zero = 00000000 One = 3F800000 NTC Oct 17, 2005 Page 12 of 12