In computer science, sign-magnitude, ones' complement, and two's complement are number representation methods used to simplify arithmetic operations on numbers in computers, especially addition and subtraction of binary numbers. Below I will briefly introduce these three representation methods:
-
Sign-Magnitude: Sign-magnitude is the most intuitive representation method. It directly uses binary numbers to represent a number, including the sign. In sign-magnitude, the most significant bit (the leftmost bit) is the sign bit, 0 indicates a positive number, and 1 indicates a negative number. The remaining bits represent the value itself. For example, the sign-magnitude representation of decimal number +5 is
0000 0101, and the sign-magnitude representation of -5 is1000 0101。 -
Ones' Complement: Ones' complement is mainly used to represent negative numbers. For positive numbers, their ones' complement is the same as their sign-magnitude. For negative numbers, their ones' complement is obtained by inverting all bits except the sign bit in the sign-magnitude representation (0 becomes 1, 1 becomes 0). For example, the ones' complement representation of decimal number -5 is
1111 1010。 -
Two's Complement: Two's complement is the most commonly used representation in computers, used for binary addition operations. For positive numbers, their two's complement is the same as their sign-magnitude. For negative numbers, their two's complement is their ones' complement plus 1. An important property of two's complement is that adding a number's two's complement to the number itself always results in 0. For example, the two's complement representation of decimal number -5 is
1111 1011。
The use of two's complement can simplify arithmetic operations in computers, because addition and subtraction can be unified into addition. When performing subtraction, you can add the two's complement of the subtrahend to the minuend to obtain the result.
Here is a simple example to illustrate two's complement operations:
- Suppose we want to calculate decimal 5 - (-3).
- First, convert the two numbers to binary: The binary of 5 is
0000 0101, because 5 is positive, its two's complement is also 00000101. The two's complement representation of -3: The sign-magnitude of 3 is 00000011, inverting it gives 11111100, adding 1 gives 11111101, so the two's complement representation of -3 is 11111101. - Then, convert the problem into addition: 5 - (-3) is equivalent to 5 + 3, that is, we need to add the two's complements of 5 and 3.
- Next, perform binary addition on the two's complements of 5 and 3:
0000 0101+0000 0011=0000 1000。 - Finally, convert the result back to decimal:
0000 1000It equals 8, which is consistent with the result of 5 + 3.
Sign-Magnitude
Concept:
- Sign-magnitude is the simplest method for representing signed numbers.
- It uses the most significant bit (the leftmost bit) to represent the sign: 0 indicates a positive number, and 1 indicates a negative number.
- The remaining bits represent the magnitude of the value.
Rules:
- For positive numbers: The sign-magnitude representation is the same as their binary value.
- For negative numbers: Take the sign-magnitude representation of the binary representation of its absolute value, and set the most significant bit to 1 (indicating negative).
Example:
- 8-bit sign-magnitude representation:
- +5:00000101
- -5:10000101
Ones' Complement
Concept:
- Ones' complement is obtained by inverting each bit of the numeric part of the sign-magnitude representation (0 becomes 1, 1 becomes 0).
- The ones' complement of a positive number is the same as its sign-magnitude.
- The ones' complement of a negative number is obtained by inverting all bits except the sign bit in its sign-magnitude representation.
Rules:
- For positive numbers, the ones' complement is the same as the sign-magnitude.
- For negative numbers, the ones' complement is obtained by inverting all bits (except the sign bit) in the sign-magnitude representation, that is, changing each binary bit from 0 to 1, or from 1 to 0.
Example:
- 8-bit ones' complement representation:
- +5:00000101
- -5:11111010
Two's Complement
Concept:
- Two's complement is obtained by adding 1 to the ones' complement.
- The two's complement of a positive number is the same as its sign-magnitude.
- The two's complement of a negative number is its ones' complement plus 1.
Rules:
- For positive numbers, the two's complement is the same as the sign-magnitude.
- For negative numbers, the two's complement is obtained by adding 1 to the ones' complement.
Example:
- 8-bit two's complement representation:
- +5:00000101
- -5: 11111011 (the ones' complement is 11111010, adding 1 gives 11111011)
Summary
- Sign-magnitude: Simple but has two zeros.
- Ones' complement: Solved some problems, but still has two zeros.
- Two's complement: The most commonly used and most efficient, has only one zero, and simplifies addition and subtraction operations in computers.
Computers widely use two's complement to represent signed integers internally, because it simplifies hardware design and operational processing. Understanding these concepts helps to understand the underlying operation mechanisms of computers and the implementation principles of some algorithms.