Although C/C ++ has been used, bit sequence is rarely involved. For example, what is the binary representation of-1? I have never studied it in depth, but it has always been ambiguous. It is really a joke.
A simple understanding of the number of signed integers is that the highest bit is the sign bit, 0 is positive, and 1 is negative. How can we express the remaining digits? Naturally, since 1 is represented as 0000 0001 (assuming an 8-bit integer number, the same below),-1 should be represented as 1000 0001.
However, in C, the correct expression of-1 should be 1111 1111, that is, 0xff. In the C language, the integer number is expressed in two's complement notation, while in my previous understanding, it is the sign-magnment notation (this method is used for floating point numbers ). In two's complement notation, 1000 0001 represents-127.
For example, the maximum value of a positive value is 0111 1111, that is, 127. The minimum negative value is 1000 0000, that is,-128.
There are also several interesting phenomena, such as the problem of converting the 8-bit signed number into the 16-bit signed number filling. In the past, it was assumed that the value is 0, but this is incorrect. It should be the value of filling the symbol bit. For example, if-1 = 0xff is filled with the symbol bit 1, it should be changed to 0 xFFFF. If the value is 0, it is incorrect to change it to 0x80ff.
Two's complement is asymmetrical. The positive and negative values of a number indicate that, except the highest bit, the other digits are also different. In the C language, the binary sequence of values does not change when there is a conversion between the unsigned number and the signed number, but only the interpretation mode of the sequence is changed. For example, if the value of "-1" is converted to "unsigned", it is converted to "255", although its binary representation is 0xff. If you do not understand this, a bug will occur.
For C/C ++ProgramIt is indeed necessary to study computer systems: a programmer's perspective ).