240 likes | 296 Views
Information Science 1 -Principles of Computing-. College of Information Science and Engineering Ritsumeikan University. Week 05. Topics covered. Terms and concepts from Week 04 Signed integer representation signed magnitude two’s complement Two’s complement arithmetic
E N D
InformationScience 1-Principles of Computing- College of Information Science and EngineeringRitsumeikan University Week 05
Topics covered • Terms and concepts from Week 04 • Signed integer representation • signed magnitude • two’s complement • Two’s complement arithmetic • arithmetic operations • arithmetic overflow • Floating point representation • IEEE 754 • Quiz
Carry (carries) • Sum, total • Borrow (borrows) • Difference • Multiplier • Product, partial product • Dividend, divisor or divider, quotient • Remainder • BCD • Gray code Recall Week 04
The objectives of this class • To understand the main conventions for representing integers in a computer • To master basic arithmetic operations in two’s complement • To understand the concept of signed arithmetic overflow • To learn the basics of real (floating point) number representation in a computer
Limitations of the simple binary representation • The computations we have so far studied (the conversions and arithmetic operations) have mostly involved positive integers • Many real-world problems, however, require dealing with both positive and negative integers, as well as with real numbers (also positive and negative) which are called in computer science floating point numbers • A computer has to perform calculations in such a way that any results obtained must be stored with the same number of bits
Integers: Signed magnitude representation • A= an-1an-2 …a0is anndigit number, wherean-1is the sign, and the remainingn-1bits are the magnitude • Ais • positive ifan-1 (which is msb) is 0 • negative ifan-1is 1 (or radix-1 for radix > 2) • Range is from-(2n-1 - 1) to (2n-1 - 1) • There are two zero representations:00…0(2 = +0 and 10…0(2 = -0
Machine word 110010.. …00101110010101 Sign bit Remaining bits for magnitude Problems with signed magnitude representation • Signed magnitude (or sign-magnitude) representation is easy for people to understand, but it is inefficient and requires complicated computer hardware. (Think: Why? Hints: Arithmetic, negative numbers, machine word size)
Machine word Sign bit 110010.. …00101110010101 How integers are actually stored: Two’s complement • When an integer is negative, it is stored in 2’s complement • Step 1. Find 1’s complement of the number by inverting all the bits: Binary: 11000110 1’s complement: 00111001 • Step 2. Add 1 to the 1’s complement: 00111001+ 00000001 = 00111010
2’s complement: Example What is -5 in 8-bit 2’s complement? Five (in unsigned binary): 0000 0101 Invert (flip) all the bits: 1111 1010 Add 1 to obtain result: 1111 1011 Check 1111 1011 sign-bit is 1 negative, “-” 0000 0100 invert all the bits 0000 0101 add one and read the result“minus five”together with the sign
Sign extension • All negative numbers have a 1 in the msb. Hardware tests only this bit to determine if the number is positive or negative • Often numbers need to be re-represented with a different number of bits (e.g. from a 16-bit machine word to a 32 word).To perform such a re-representation, duplicate the sign in all bits to the left in the larger representation of a number:00100000 00100000 0000 0010 …10101111 10101111 1111 1010 …
2’s complement in hexadecimal 1.Find the “opposite” hex digit on the chart at right for every digit in the number 2. Add 1 to the result 0 1 2 3 4 5 6 7 F E D C B A 9 8 For example: 123C EDC3 Add 1 to the result: EDC4 If we would do it in binary: 1 2 3 C 0001 0010 0011 1100 1110 1101 1100 0011 1110 1101 1100 0100 E D C 4
2’s complement arithmetic • All arithmetic operations can be performed on two’s complement representations, using positive, modulo 2n arithmetic (i.e. exactly as with unsigned binary) • When subtracting A - B, convert B to its 2's complement, and then add A to (-B): 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 - 0 0 0 0 0 0 1 1 + 1 1 1 1 1 1 0 1 0 0 0 0 1 0 0 1
5-bit 2’s complement: 0 0 0 1 1 1 1 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 1 0 1 0 0 0 1 0 0 1 0 1 0 0 0 1 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 2’s complement range • The range of numbers represented in 2’s complement is determined by n, the number of bits available, and isfrom-(2n-1) to (2n-1 - 1) • When doing 2’s complement arithmetic, remember that all the numbers must be kept within the allowed range = 81? No. It’s = -15
Problems with 2’s complement arithmetic • Compute 32,767+1 in 16-bit 2’s complement • 32,767 (or 7FFFh) • 0111 1111 1111 1111 • + 0000 0000 0000 0001 • 1000 0000 0000 0000 (or 8000h) • which is –32768 ?! • The result of 32,767+1 cannot be represented with a 16-bit signed number, because it is out of range. If you still attempt to do so, a “signed arithmetic overflow” error will occur
2’s complement overflow:A 4-bit example 0 -1 +1 0000 1111 0001 -2 +2 1110 0010 -3 +3 1101 0011 -4 1100 0100 +4 1011 0101 -5 +5 1010 0110 -6 +6 1001 0111 1000 -7 +7 -8
2’s complement overflowdetection • Detection rules. Overflow occurs if: • Overflow occurs if 1) there is a carry in to the sign bit, but no carry out of it, or2) there is a carry out of the sign bit, but no carry in to it
1 8 23 S exponent fraction Real numbers: Floating point representation • Often, floating point representation uses the IEEE 754 (32-bit) standard: • 1 bit for the sign (0 for “+” and 1 for “-”) • 8 bits for the magnitude (exponent) • 23 bits for the precision (fraction or significand)
1 8 23 “floating” point S exponent fraction Thus the exponent is given by: Floating point representation 1 10000001 10101000000000000000000
1 8 23 S exponent fraction Floating point: The largest and the smallest numbers 0 11111110 11111111111111111111111 0 00000000 00000000000000000000001
Floating point arithmetic • Before doing floating point arithmetic, the exponents for both numbers must be appropriately adjusted • When converting base 10 fractions to binary, only those fractions whose values can be expressed as a sum of base 2 fractions will convert evenly • All other base 10 fractions feature a least significant bit that is either rounded or truncated – an approximation • When two such numbers are multiplied, the rounding error is compounded
Special floating point representations • An exponent equal 255 = 11111111(2 in a floating point representation indicates a special value • When the exponent is equal 255 = 11111111(2 and the fraction is 0, the value represented is infinity • When the exponent is equal 255 = 11111111(2 and the fraction is non-zero, the value represented is Not a Number (NaN)
Summary • In a computer, (negative) integers are usually represented in 2’s complement • 2’s complement arithmetic is same as for unsigned binary. A special care is, however, needed to avoid arithmetic overflow • Real numbers are represented using the floating point format • Floating point arithmetic requires more complicated operations than in the case of 2’s complement
Homework • Get the homework fromhttp://www.ritsumei.ac.jp/is/~rinaldo/InfoScience/InfoScience1.html • Read these slides again and learn the new terms • Read the corresponding material in the textbook
Next class • Program Development • Algorithms and Problem Solving