When there are signed and unsigned numbers in the operation expression, the complement code remains unchanged, and the signed number becomes an unsigned number.

1. Original code, inverse code and complement code

1. Original code

The highest bit represents the sign, and the remaining bits represent the value. For example, in an 8-bit binary number, the highest bit is the sign bit, 0 means a positive number, and 1 means a negative number. For the integer -5, the binary number of 5 is 0000 0101, the sign bit is 1, its 8-bit original code is expressed as 10000101, the eight-bit original code of -3 is 10000011, and the eight-bit original code of -20 is 10010100. In the original code representation, the value of the negative number is different from the positive number, and the sign bit will affect the correctness of the arithmetic operation.

2. Inverse code

Inverse code is one of the earliest representations of signed integers. Its basic idea is to represent the value in binary and at the same time use the highest bit to represent the sign bit, 0 to represent a positive number, and 1 to represent a negative number. In the inverse code, the inverse code of a positive number is the same as the original code, and the inverse code of a negative number is to invert all bits in the original code except the sign bit, that is, 1 becomes 0, and 0 becomes 1.

For example, an 8-bit signed number, if its original code is 10110110, then its sign bit is 1, indicating a negative number, and its inverse code is 11001001.

3. Complement

Complement code is an improvement of one's complement code. Its basic idea is to change the representation of negative numbers from the original inversion to inversion and then add 1. In the complement code, the complement code of the positive number is the same as the original code, and the complement code of the negative number is to invert all bits in the original code except the sign bit, and then add 1.

For example, an 8-bit signed number, if its original code is 10110110, then its sign bit is 1, indicating a negative number, and its complement is 11001010.

The advantage of complement code is that it can implement subtraction through an adder, because subtraction can be converted into addition, and the complement code of negative numbers can just meet the requirements of this adder. Therefore, in computers, signed integers are usually represented in two's complement.

It should be noted that in the complement code, the complement code of the smallest negative number (1000 0000 in 8 bits, 1 is the sign bit, and the smallest negative number is 1000 0000=128) cannot be represented by the original code, because the smallest negative number The absolute value is 1 greater than the absolute value of the largest positive number (the sign bit is 0, and the largest positive number is 0111 1111-1=128-1=127). Therefore, it is usually stipulated in computers that the complement representation of an n-bit binary number ranges from -2^(n-1) to 2^(n-1)-1, and the absolute value of the smallest negative number is 2^(n- 1).

4. Example

(1) Convert the original code to the inverse code

For example, an 8-bit signed number, if its original code is 10110110, then its sign bit is 1, indicating a negative number. Its inverse code is to invert all bits except the sign bit, that is, 11001001.

(2) Convert original code to complementary code

For example, an 8-bit signed number, if its original code is 10110110, then its sign bit is 1, indicating a negative number. Its complement code is to invert all bits in the original code except the sign bit, that is, 11001001, and then add 1, that is, 11001010.

(3) Reverse code to original code

For example, an 8-bit signed number, if its one's complement is 11001001, then its sign bit is 1, indicating a negative number. When converting it to the original code, it is necessary to keep the sign bit unchanged, and invert all bits except the sign bit, that is, 10110110.

(4) Complementary code to original code

For example, an 8-bit signed number, if its complement is 11001010, then its sign bit is 1, indicating a negative number. When converting it to the original code, it needs to be subtracted by 1, which is 11001001, and then all bits except the sign bit are inverted, which is 10110110.

It should be noted that in actual programming, the computer will use the corresponding encoding method according to the definition of the data type, and developers do not need to manually convert.

2. There are both signed and unsigned numbers in the operation expression

In an operation expression, if there are signed numbers and unsigned numbers at the same time, type conversion will be performed according to the following rules:

  1. If signed and unsigned numbers are of the same type, then operations can be performed directly without conversion.

  2. If the signed and unsigned numbers are of different types, the signed number will be converted to an unsigned number, and the method of converting a signed number to an unsigned number: signed number + modulo of an unsigned number.

For the conversion of signed numbers to unsigned numbers, if the signed number is positive, then its complement is its binary representation; if the signed number is negative, then its complement is the binary representation of its absolute value . Add 1 more . It should be noted that when a signed number is converted to an unsigned number, if its value is negative, the result will become a very large positive number, because there is no concept of negative numbers in unsigned numbers, all All bits are used to represent non-negative integers, so conversion from signed to unsigned requires care.

Here are some examples to illustrate the process of converting signed numbers to unsigned numbers:

  1. The signed number is -3, and the complement of -3 converted to an 8-bit unsigned number is 11111101. When converting it to an unsigned number, the complement needs to be regarded as an unsigned number, that is, 11111101 = 253, so -3 conversion The result of an 8-bit unsigned number is 253.
  2. 2 The signed number is 127, and the complement code of 127 converted into an 8-bit unsigned number is 01111111. When it is converted into an unsigned number, the complement code remains unchanged, that is, 01111111 = 127, so 127 is converted into an 8-bit unsigned number The result is 127.
  3.                                       The signed number is -128, and the complement of -128 converted into an 8-bit unsigned number is 10000000. When converting it into an unsigned number, you need to treat the complement as a signed number first, that is, -128 = 2^7 = 128, and then convert it to an unsigned number, that is, 128 = 0, so the result of converting -128 to an 8-bit unsigned number is 0. It should be noted that when converting a signed number to an unsigned number, an appropriate range needs to be selected according to the number of bits of the data type to avoid problems such as overflow.

Conversion of signed and unsigned numbers

For example :

(1) complement code

20 in binary is

 

After taking the inverse and adding 1, the complement code is

converted to an unsigned number as

 (2) Modulus of signed number + unsigned number

          Add 2^n (n bits), -20+2^32=-20+4294967296=4294967276

 

 

Guess you like

Origin blog.csdn.net/qq_44732869/article/details/129967645