Beruflich Dokumente
Kultur Dokumente
In some applications, data either positive or negative. We concentrate on absolute value or magnitude only. For example : Smallest 8-bit number is 0000 0000 Largest 8-bit number is 1111 1111 Equivalent to a decimal 0 to 255.
Data is called unsigned binary because all bits are used to represent magnitude of the corresponding decimal only Limitations: With 8-bit unsigned arithmetic, all magnitudes must fall in the range 0 to 255 Addition and subtraction must also fall between 0 and 255. If greater than 255, use 16-bit arithmetic
OVERFLOW
In 8-bit arithmetic , addition of 2 unsigned numbers whose sum is greater than 255 , causes overflow. a carry into 9th column Microprocessors have carry flag to detect a carry which warns , answer is invalid.
Example
Addition of 175
10
and 118
10
175 1010 1111 118 0111 0110 293 1 0010 0101 In case of overflow, programmer has to carry instructions for carry flag and use 16-bit arithmetic.
We have been ignoring signed integers. The PROBLEM with signed integers ( 45, + 27, -99) is the SIGN! How do we encode the sign? The sign is an extra piece of information that has to be encoded in addition to the magnitude.
Signed Magnitude (SM) is a method for encoding signed integers. The Most Significant Bit (MSB) is used to represent the sign. 1 is used for a - (negative sign), 0 for a + (positive sign).
Format of SM Representation
The format of a SM number in 8 bits is: smmmmmmm Where s is the sign bit The other 7 bits represent the magnitude. NOTE: for positive numbers, the result is the same as the unsigned binary representation.
-5 = (1 0000101)2 = (85)16 +5 = (0 0000101)2 = (05)16 +127 = (0 1111111)2 = (7F)16 -127 = (1 1111111)2 = (FF)16 +0 = (0 0000000)2 = (00)16 -0 = (1 0000000)2 = (80)16
For N bits, can represent the signed integers - {2(N-1) 1} to {+ 2(N-1) 1} For 8 bits, can represent the signed integers -127 to +127. Signed magnitude easy to understand and encode. Simple. Negative numbers are identical to positive numbers except the sign bit. Used today in some applications.
One problem is that it has two ways of representing 0 (-0,and +0) Another problem is that addition of K + (-K) does not give Zero! -5 + 5 = (85)16 + (05)16 = (8A)16 Requires complicated arithmetic circuits.
Ones complement is another way to represent signed integers. To encode a negative number, get the binary representation of its magnitude, then COMPLEMENT each bit. Complementing each bit mean that 1s are replaced with 0s, 0s are replaced with 1s.
Example:
How is -5 represented in Ones Complement (encoded in 8 bits) ? The magnitude 5 in 8-bits is ( 00000101)2 = (05)16 Now complement each bit: (11111010)2 = (FA)16 (FA)16 is ones complement representation of -5.
+5 -5 +127 -127 +0 -0
= = = = = =
(00000101)2 = ( 05 )16 (11111010) = ( FA )16 (01111111)2 = ( 7F )16 (10000000)2 = ( 80 )16 (00000000)2 = ( 00 )16 (11111111)2 = ( FF )16
For N bits, can represent the signed integers - {2(N-1) 1} to + {2(N-1) 1} For 8 bits, can represent the signed integers -127 to +127.
Still have the problem that there are two ways of representing 0 (-0, and +0) Mathematically speaking, no such thing as two representations for zeros. However, addition of K + (-K) now gives Zero! -5 + 5 = ( FA )16 + ( 05 )16 = ( FF)16 = -0 !!!
Unfortunately, K + 0 = K only works if we use +0, Does not work if we use -0. 5 + (+0) = (05)16 + (00)16 = (05)16 = 5 (ok) 5 + (-0) = (05)16 + (FF)16= (04)16 = 4 (wrong)
Twos complement is another way to represent signed integers. To encode a negative number, get the binary representation of its magnitude, COMPLEMENT each bit, then ADD 1. or See 1 from LSB , mark it and complement all bits which are occurring before it.
Example
What is -5 in Twos Complement, 8 bits? The magnitude 5 in 8-bits is: (00000101)2 = (05)16 Now complement each bit: (11111010)2 = (FA)16 Now add one: (FA) 16 + 1 = (FB)16 (FB)16 is the 8-bit, twos complement representation of -5.
-5 = ( 11111011) = ( FB)16 +5 = ( 00000101 )= ( 05)16 +127 = ( 01111111) = (7F)16 -127 = ( 10000001) = ( 81)16 -128 = ( 10000000 )= ( 80 )16 + 0 = ( 00000000) = (00)16 -0 = ( 00000000) = (00 )16 (only 1 zero!!!)
For N bits, can represent the signed integers -2(N-1) to + 2(N-1) - 1 Note that negative range extends one more than positive range. For 8 bits, can represent the signed integers -128 to +127.
Twos complement is the method of choice for representing signed integers. It has none of the drawbacks of Signed Magnitude or Ones Complement. There is only one zero, and K + (-K) = 0. -5 + 5 = (FB)16 + ( 05)16 = (00)16 = 0 !!! Normal binary addition is used for adding numbers that represent twos complement integers.
Complements
Used in digital computer for simplifying the subtraction operation and for logic manipulations. There are two types of complements for each base R system
(1) The rs complement. e.g. 2s complement for binary and 10s complement for decimal.
(2) The (r-1)s complement. e.g. 1s complement for binary and 9s complement for decimal.
where = ( Rn R-m ) N,
n = Total number of digits in integer part of the number N m = Total number of digits in fractional part of the number N
Examples
9s complements of (52520)10 (105-100-52520)= (105-152520)=47479 9s complements of (0.3267)10 (100-10-4-0.3267)= (0.9999- 0.3267) =0.6732
+ve nos.: sign and magnitude -ve nos. : -N is represented by N*, the R's Complement n where N* = R N n = Total number of digits in integer part of the number N
Examples:
10s complements of (52520)10 (105-52520) = (105-52520) = 47480 10s complements of (0.3267)10 (100-0.3267) = (1.0- 0.3267) = 0.6733