## Programming
## Definition**Property**- Two's complement representation allows the use of binary arithmetic operations on signed integers, yielding the correct 2's complement results.
**Positive Numbers**- Positive 2's complement numbers are represented as the simple binary.
**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.
**Note:**The most significant (leftmost) bit indicates the sign of the integer; therefore it is sometimes called the sign bit.- If the sign bit is zero,
- then the number is greater than or equal to zero, or positive.
- If the sign bit is one,
- then the number is less than zero, or negative.
## Calculation of 2's ComplementTo 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
## 2's Complement AdditionTwo's complement addition follows the same rules as
## 2's Complement SubtractionTwo's complement subtraction is the
## 2's Complement MultiplicationTwo's complement multiplication follows the same rules as
## 2's Complement DivisionTwo's complement division is repeated
## Sign ExtensionTo extend a signed integer from 8 bits to 16 bits or from 16 bits to 32 bits, append additional bits on the left side of the number. Fill each extra bit with the value of the smaller number's most significant bit (the sign bit).
## Other Representations of Signed Integers**Sign-Magnitude Representation**- Another method of representing negative numbers is sign-magnitude. Sign-magnitude representation also uses the most significant bit of the number to indicate the sign. A negative number is the 7-bit binary representation of the positive number with the most significant bit set to one. The drawbacks to using this method for arithmetic computation are that a different set of rules are required and that zero can have two representations (+0,
`0000 0000`and -0,`1000 0000`). **Offset Binary Representation**- A third method for representing signed numbers is offset binary. Begin calculating a offset binary code by assigning half of the largest possible number as the zero value. A positive integer is the absolute value added to the zero number and a negative integer is subtracted. Offset binary is popular in A/D and D/A conversions, but it is still awkward for arithmetic computation.
*For example,* Largest value for 8-bit integer = 2^{8}= 256Offset binary zero value = 256 ÷ 2 = 128 _{(decimal)}=`1000 0000`_{(binary)}`1000 0000`_{(offset binary 0)}`+ 0001 0110`_{(binary 22)}`= 1001 0110`_{(offset binary +22)}`1000 0000`_{(offset binary 0)}`- 0000 0111`_{(binary 7)}`= 0111 1001`_{(offset binary -7)}
## Notes**Other Complements**- 1's Complement =
`NOT`(*n*) =`1111 1111`-*n* - 9's Complement =
`9999 9999`-*n* - 10's Complement = (
`9999 9999`-*n*) + 1 **Binary Arithmetic****Addition****Subtraction****Multiplication****Division**
Index | Technical Notes ]Page author: Dawn Rorvik (rorvikd@evergreen.edu) Last modified: 05/20/2003 |