Chapter 3 Data Representation part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010
Outline Subtraction of Unsigned Numbers using r’s complement How To Represent Signed Numbers Floating-Point Representation
Subtraction of Unsigned Numbers using r’s complement (1) if M N, ignore the carry without taking complement of sum. (2) if M < N, take the r’s complement of sum and place negative sign in front of sum. The answer is negative.
Example 1 (Decimal unsigned numbers), perform the subtraction = M > N : “Case 1” “Do not take complement of sum and discard carry” The 10’s complement of is Therefore: M = ’s complement of N = Sum= Discard end carry 10 5 = Answer = 59282no complement
Example 2; Now consider an example with M <N. The subtraction produces negative Using the procedure with complements, we have M = ’s complement of N = Sum = Take 10’s complement of Sum = The number is : Place negative sign in front of the number:-59282
Subtract by Summation Subtraction with complement is done with binary numbers in a similar way. Using two binary numbers X= and Y= We perform X-Y and Y-X
X-Y X= ’s com. of Y= Sum= Answer=
Y-X Y= ’s com. of X= Sum= There’s no end carry: answer is negative (2’s complement of )
Outline Subtraction of Unsigned Numbers using r’s complement How To Represent Signed Numbers Floating-Point Representation
How To Represent Signed Numbers Plus and minus signs used for decimal numbers: 25 (or +25), -16, etc.. For computers, it is desirable to represent everything as bits. Three types of signed binary number representations: 1. signed magnitude, 2. 1’s complement, and 3. 2’s complement
1. signed magnitude In each case: left-most bit indicates sign: positive (0) or negative (1). Consider 1. signed magnitude: = Sign bitMagnitude = Sign bitMagnitude
2. One’s Complement Representation The one’s complement of a binary number involves inverting all bits. To find negative of 1’s complement number take the 1’s complement of whole number including the sign bit.To find negative of 1’s complement number take the 1’s complement of whole number including the sign bit = Sign bitMagnitude = Sign bit1’complement
3. Two’s Complement Representation The two’s complement of a binary number involves inverting all bits and adding 1. To find the negative of a signed number take the 2’s the 2’s complement of the positive number including the sign bit = Sign bitMagnitude = Sign bit2’s complement
The rule for addition is add the two numbers, including their sign bits, and discard any carry out of the sign (leftmost) bit position. Numerical examples for addition are shown below. Example: In each of the four cases, the operation performed is always addition, including the sign bits. Only one rule for addition, no separate treatment of subtraction. Negative numbers are always represented in 2’s complement. Sign addition in 2’s complement
Arithmetic Subtraction A subtraction operation can be changed to an addition operation if the sign of the subtrahend is changed. (±A) - (+B) = (±A) + (-B) (±A) - (-B) = (±A) + (+B)
Arithmetic Subtraction Consider the subtraction of (-6) - (-13) = +7. In binary with eight bits this is written as The subtraction is changed to addition by taking the 2’s complement of the subtrahend (-13) to give (+13). In binary this is = Removing the end carry, we obtain the correct answer (+ 7).
Overflow Overflow example: = =
Overflow An overflow cannot occur after an addition if one number is positive and the other is negative, since adding a positive number to a negative number produces a result that is smaller than the larger of the two original numbers. An overflow may occur if the two numbers added are both either positive or negative.
Outline Subtraction of Unsigned Numbers using r’s complement How To Represent Signed Numbers Floating-Point Representation
is represented in floating-point with a fraction and an exponent as follows: Fraction Exponent Scientific notation : 10 +4
Floating-Point Representation 32-bit floating point format. Leftmost bit = sign bit (0 positive or 1 negative). Exponent in the next 8 bits. Use a biased representation. Final portion of word (23 bits in this example) is the significand (sometimes called mantissa).
Example Convert the following number;37.75 into floating point format to fit in 32 bit register. Convert the number from decimal into binary Normalize all digits including the fraction to determine the exponent x sign EXP Significant
Outline Subtraction of Unsigned Numbers using r’s complement How To Represent Signed Numbers Floating-Point Representation