160 likes | 171 Views
Learn how computers represent signed numbers using conventions like sign-magnitude, two's complement, and more. Explore arithmetic operations and practical applications in computer math.
E N D
CPS120: Introduction to Computer Science Computer Math: Signed Numbers
Representing Signed Numbers • Remember, all numeric data is represented inside the computer as 1s and 0s • Arithmetic operations, particularly subtraction raise the possibility that the result might be negative • Any numerical convention needs to differentiate two basic elements of any given number, its sign and its magnitude • Conventions • Sign-magnitude • Ten's complement • Two’s complement • One’s complement
Representing Negatives • It is necessary to choose one of the bits of the “basic unit” as a sign bit • Usually the leftmost bit • By convention, 0 is positive and 1 is negative • Positive values have the same representation in all conventions • However, in order to interpret the content of any memory location correctly, it necessary to know the convention being used used for negative numbers
Representing Negative Values • You have used the signed-magnitude representation of numbers since grade school. The sign represents the ordering, and the digits represent the magnitude of the number.
Representing Negative Values (Cont’d) • There is a problem with the sign-magnitude representation: • There are two representations of zero. • There is plus zero and minus zero. Two representations of zero within a computer can cause unnecessary complexity.
Sign-Magnitude • For a basic unit of N bits, the leftmost bit is used exclusively to represent the sign • The remaining (N-1) bits are used for the magnitude • The range of number represented in this convention is –2 N+1 to +2 N-1 -1
Sign-magnitude Operations • Addition of two numbers in sign-magnitude is carried out using the usual conventions of binary arithmetic • If both numbers are the same sign, we add their magnitude and copy the same sign • If different signs, determine which number has the larger magnitude and subtract the other from it. The sign of the result is the sign of the operand with the larger magnitude • If the result is outside the bounds of –2 n+1 to +2 n-1 –1, an overflow results
Other Ways of Representing Negative Values • If we allow only a fixed number of values, we can represent numbers as just integer values, where half of them represent negative numbers. • For example, if the maximum number of decimal digits we can represent is two, we can let 1 through 49 be the positive numbers 1 through 49 and let 50 through 99 represent the negative numbers -50 through -1.
Representing Negative Values (Cont’d) • To perform addition within this scheme, you just add the numbers together and discard any carry.
Representing Negative Values (Cont’d) • A-B=A+(-B). We can subtract one number from another by adding the negative of the second to the first.
Representing Negative Values (Cont’d) • There is a formula that you can use to compute the negative representation: • This representation of negative numbers is called the ten’s complement.
Representing Negative Values (Cont’d) Two’s Complement:To make it easier to look at long binary numbers, we make the number line vertical.
Representing Negative Values (Cont’d) • Addition and subtraction are accomplished the same way as in 10’s complement arithmetic: -127 10000001 + 100000001 -126 10000010 • Notice that with this representation, the leftmost bit in a negative number is always a 1.
Two’s Complement Convention • A positive number is represented using a procedure similar to sign-magnitude • To express a negative number • Express the absolute value of the number in binary • Change all the zeros to ones and all the ones to zeros (called “complementing the bits”) • Add one to the number obtained in Step 2 • The range of negative numbers is one larger than the range of positive numbers • Given a negative number, to find its positive counterpart, use steps 2 & 3 above
One’s Complement • Devised to make the addition of two numbers with different signs the same as two numbers with the same sign • Positive numbers are represented in the usual way • For negatives • STEP 1: Start with the binary representation of the absolute value • STEP 2: Complement all of its bits