Upcoming SlideShare
×

# Two’s complement

13,654 views

Published on

report ni jeffrey cinco

1 Comment
7 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• This site has got good staff made by smart people in did. I liked the way two`s complement was described.

Are you sure you want to  Yes  No
Views
Total views
13,654
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
316
1
Likes
7
Embeds 0
No embeds

No notes for slide

### Two’s complement

1. 2. Definition <ul><li>Property Two's complement representation allows the use of binary arithmetic operations on signed integers, yielding the correct 2's complement results. </li></ul><ul><li>Positive Numbers Positive 2's complement numbers are represented as the simple binary. </li></ul><ul><li>Negative Numbers Negative 2's complement numbers are represented as the binary number that when added to a positive number of the same magnitude equals zero. </li></ul>
2. 3. Signed Numbers <ul><li>The most significant (leftmost) bit indicates the sign of the integer; therefore it is sometimes called the sign bit . </li></ul><ul><li>If the sign bit is zero, then the number is greater than or equal to zero, or positive. </li></ul><ul><li>If the sign bit is one, then the number is less than zero, or negative. </li></ul>
3. 4. Calculation of Two’s Compliment <ul><li>To calculate the 2's complement of an integer, invert the binary equivalent of the number by changing all of the ones to zeroes and all of the zeroes to ones (also called 1's complement ), and then add one. </li></ul>
4. 5. Cont… <ul><li>These examples show conversion of a decimal number to 8-bit twos complement. </li></ul><ul><li>The bit size is always important with twos complement, since you must be able to tell where the sign bit is. The steps are simple. </li></ul><ul><li>First, you convert the magnitude of the number to binary, and pad to the word size (8 bits). If the original number was positive, you are done. Otherwise, you must negate the binary number by inverting the bits and adding 1. </li></ul>
5. 6. Convert -72 <ul><li>Convert -72 to an 8-bit, twos complement binary number. Convert the magnitude, 72 to binary. The easiest way is to convert it to hex first. 72÷16 = 4 remainder 8, so 72 10 = 48 16 = 1001000 2 . </li></ul><ul><li>Pad to 8 bits: 01001000 </li></ul><ul><li>Negate the number by inverting the bits and adding 1. </li></ul>So, -72 10 is 10111000 as an eight-bit, two's complement number.
6. 7. Two’s Compliment of Addition <ul><li>Two's complement addition follows the same rules as binary addition . </li></ul><ul><li>For example, </li></ul>
7. 8. 2's Complement Subtraction <ul><li>Two's complement subtraction is the binary addition of the minuend to the 2's complement of the subtrahend (adding a negative number is the same as subtracting a positive one). </li></ul><ul><li>For example, </li></ul>
8. 9. 2's Complement Multiplication <ul><li>Two's complement multiplication follows the same rules as binary multiplication . </li></ul><ul><li>For example, </li></ul>
9. 10. 2's Complement Division <ul><li>Two's complement division is repeated 2's complement subtraction . </li></ul><ul><li>The 2's complement of the divisor is calculated, then added to the dividend. For the next subtraction cycle, the quotient replaces the dividend. This repeats until the quotient is too small for subtraction or is zero, then it becomes the remainder. </li></ul><ul><li>The final answer is the total of subtraction cycles plus the remainder. </li></ul>
10. 11. Two's Complement Overflow Rules <ul><li>The rules for detecting overflow in a two's complement sum are simple: </li></ul><ul><li>1. If the sum of two positive numbers yields a negative result, the sum has overflowed. </li></ul><ul><li>2. If the sum of two negative numbers yields a positive result, the sum has overflowed. </li></ul><ul><li>3. Otherwise, the sum has not overflowed. </li></ul>
11. 12. Cont… <ul><li>It is important to note the overflow and carry out can each occur without the other. In unsigned numbers, carry out is equivalent to overflow. In two's complement, carry out tells you nothing about overflow. </li></ul>
12. 13. Cont… <ul><li>The reason for the rules is that overflow in two's complement occurs, not when a bit is carried out of the left column, but when one is carried into it. That is, when there is a carry into the sign. The rules detect this error by examining the sign of the result. A negative and positive added together cannot overflow, because the sum is between the addends. Since both of the addends fit within the allowable range of numbers, and their sum is between them, it must fit as well. </li></ul>
13. 14. Examples