1 / 16

CPS120: Introduction to Computer Science

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.

elmerthomas
Download Presentation

CPS120: Introduction to Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPS120: Introduction to Computer Science Computer Math: Signed Numbers

  2. 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

  3. 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

  4. Comparing the Conventions

  5. 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.

  6. 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.

  7. 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

  8. 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

  9. 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.

  10. Representing Negative Values (Cont’d) • To perform addition within this scheme, you just add the numbers together and discard any carry.

  11. 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.

  12. 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.

  13. Representing Negative Values (Cont’d) Two’s Complement:To make it easier to look at long binary numbers, we make the number line vertical.

  14. 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.

  15. 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

  16. 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

More Related