In-depth understanding of the forced conversion of signed and unsigned numbers

        In the computer system, the value is always stored in the complement code, because the sign bit and the value field can be processed uniformly by using the complement code, and the addition and subtraction can be processed uniformly. However, what we usually see are binary original codes. The following are the concepts of original codes, inverse codes, and complement codes.

Original code: It is a binary fixed-point representation method for numbers in a computer, which is normally used in binary representation.

Inverse code: The inverse code of a positive number is the same as its original code; the inverse code of a negative number is the inverse of its original code bit by bit, except for the sign bit.

Complement code: The complement code of a positive number is the same as its original code; the complement code of a negative number is equal to its inverse code + 1.

1. Why does the integer value range of 8-bit signed type in C language range from -128 to 127

      The range of 8-bit unsigned numbers is: 0 ~ 255. There is no doubt about it. The original binary code is expressed as 1111 1111, because unsigned numbers have no sign bit, and because it is a positive number, the calculation result is 255.

      According to the above, the range of the signed type can be intuitively expressed as 1111 1111~0111 1111, the highest bit is the sign bit, so the result is -127~127, but how did -128 come from?

      At the beginning we mentioned that computers use complement codes to store data, and the original code is only shown to us intuitively, so if we want to understand how -128 comes from, we must start from the direction of complement codes. In a computer, not only the most significant bit in the original code is the sign bit, but the most significant bit in the complement code is also the sign bit, and the two sign bits are both the same and different. Let's take the 4-bit signed type as an example to explain how the complement data is represented.

      In the original code, the highest bit (sign bit) only indicates positive and negative, and the other bits indicate values. For example: 1001, the value is -1.

      In the complement code, the highest bit (sign bit) not only indicates the positive or negative, but also the value, the same: 1001, the value in the complement code is -7.

      And this is the crux of the problem. Do you think how to convert 1001 in the complement code to -7 in the decimal system? Do you want to change from the complement code to the inverse code and then to the original code, 1001->1000->1111==- 7. In fact, the complement code can also be directly calculated. In the complement code, the highest bit is 1, its highest bit is a negative number, and the other bits are all positive numbers, and the calculation result of the complement code is the value of the highest bit plus the value of other bits. For example, 1001 in complement code is actually -1*2 to the 3rd power + 0*2 to the 2nd power + 0*2 to the 1st power + 1 = -8+0+0+1=-7. Another example is 0101 in complement code, which is -0*2 to the 3rd power + 1*2 to the 2nd power + 0*2 to the 1st power + 1=0+4+0+1=5.

      Now that everyone understands the method of directly converting two's complement to decimal, what do you think is the maximum and minimum value of the two's complement of a 4-bit signed number? That's right, the minimum value is 1000, and the maximum value is 0111, which is -8~7. In the original code, it is indeed 1111~0111, which is -7~7. But the computer is really stored in complement code, so the range of representation must be -8~7, and -8 has neither original code nor inverse code, it only has complement code. Substituting the above results into our 8-bit signed number, we know that -128~127 is not the range represented by the original code, but the range of the complement code in the computer, and -128 has no real original code representation.

2. In-depth understanding of the forced conversion between signed and unsigned numbers.

      In fact, everyone has almost understood the principle of forced conversion after reading this, because the signed numbers we usually see are all original codes, and those stored in the computer are complement codes, so when a number is forced to be converted, it The value of may change, but its bit pattern remains unchanged, that is, its complement in the computer remains unchanged. Let's give an example: the original code of the signed number -3 is 1011, its inverse code is 1100, and its complement code is 1101. When it is converted to an unsigned type, the complement code remains unchanged, and because the original code of the unsigned number is both the complement and the complement code, so it is converted to the binary number of the unsigned number is 1101->1101->1101, and the unsigned number There is no sign bit, and the value of each bit adds up to 13, so the signed number -3 is converted to the unsigned number 13. I believe that everyone has understood the principle of conversion through the above examples, and then everyone is looking for a signed number greater than 0 and converting it to an unsigned number. You will find that the values ​​are the same, so the following rules are drawn:

       When signed number<0, unsigned number=signed number+2 to the power of n (n is the number of bits in binary)

       When signed number > 0, unsigned number = signed number

Only by understanding the principles of computers can you learn new knowledge better. If you do calculations normally, you just need to remember the final conclusion.

To sum up, I think everyone should understand almost the same. If anyone still has some questions, they can put them in the comment area to discuss together.

Friends who feel helpful give a thumbs up

Guess you like

Origin blog.csdn.net/H0893_888/article/details/131815771
Recommended