Title: CPS120: Introduction to Computer Science
1CPS120 Introduction to Computer Science
- Computer Math Signed Numbers
2Representing 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
- Twos complement
- Ones complement
3Representing 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
4Comparing the Conventions
5Representing 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.
6Representing Negative Values (Contd)
- 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.
7Sign-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 N1 to 2 N-1 -1
8Sign-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 n1 to
2 n-1 1, an overflow results
9Other 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.
10Representing Negative Values (Contd)
- To perform addition within this scheme, you just
add the numbers together and discard any carry.
11Representing Negative Values (Contd)
- A-BA(-B). We can subtract one number from
another by adding the negative of the second to
the first.
12Representing Negative Values (Contd)
- There is a formula that you can use to compute
the negative representation - This representation of negative numbers is called
the tens complement.
13Representing Negative Values (Contd)
Twos Complement To make it easier to look at
long binary numbers, we make the number line
vertical.
14Representing Negative Values (Contd)
- Addition and subtraction are accomplished the
same way as in 10s complement arithmetic - -127 10000001
- 1 00000001
- -126 10000010
- Notice that with this representation, the
leftmost bit in a negative number is always a 1.
15Twos 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
16Ones 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