This thing has always been ignored in the actual programming, the range is small to change a large point of representation, but always feel that the basic knowledge is still well mastered, so that the time to use shift operations or type conversion or pen questions to take a moment to think.
The basic types of C languages are char, int, float, double, and also specifier long, short, signed, and unsigned.
The first thing to note is that in different operating systems the type size is not the same, the following scenario is only one case.
both int and char default to signed, the highest one in the binary to represent the symbol, and 0 for positive 1 as negative.
If the short int is 16 bits, because the 1th digit is positive or negative, only 15 bits are left to represent the actual value, ranging from -2^15 to 2^15-1
For example, according to the original code:
0000 0000 0000 0101 means 5
1000 0000 0000 0101 Means-5
The inverse code is the symbol bit unchanged, the value is reversed, for example, 5 is expressed as 0111 1111 1111 1010
But that's the problem, 1000 0000 0000 0000 with 0000 0000 0000 0000 is 0, so 0 is encoded in 2 ways.
So the C language takes the complement, 1000 0000 0000 0000 means -2^15, not 0.
complement : 1, for positive, complement the same as the original code, 2, for negative numbers, the absolute value of the number of digits after the lowest bit plus 1.
Therefore, the C language is used to represent-5 is 1111 1111 1111 1011
Then the negative integer successive self-addition operation results are as follows
-4 1111 1111 1111 1100
-3 1111 1111 1111 1101
-2 1111 1111 1111 1110
-1 1111 1111 1111 1111
Thus 1 the representation of all the digits to 0,0 becomes 0000 0000 0000 0000, representing only one form of 0.
Char is represented inside the computer by a byte binary, which assumes the default is signed, which represents a range of 128 to 127.
for char c = 128; The binary representation of C is 1000 0000 If the conversion to an int output is-127.
char conversion to short int does not mean that the number of digits is increased, but rather it is interpreted as a short int, so C is still 1000 0000, which means 127, not because the transformation to int becomes 0000 0000 1000 0000
Look at the following code, signed turn unsigned
int _tmain (int argc, _tchar* argv[]) {char c = 128;unsigned char cu = c;short int i = Cu;cout << i;return 0;}
Convert c to unsigned char, then converted to short int, then the output is 128, after the transformation is 1000 0000, but according to unsigned's explanation, the highest bit is no longer the sign bit, but the value bit, so the result is 2^8=128.
Well, look at the following section of code, unsigned turn signed
int _tmain (int argc, _tchar* argv[]) {unsigned char cu = 255;char c = cu;short int i = C;cout << i;return 0;}
The unsigned of 255 is expressed as 1111 1111, and after conversion to signed, the symbol bit 1 represents a negative number, the value bit is converted to decimal and 127, and the result is-1, as defined by the complement.
Before using OPENCV processing image, often be around, because Iplimage* ImageData is a char, and the processing of the image is generally converted to unsigned char, review the concept of the complement of the following this corresponds to a good understanding.
Numerical range |
[0,127] |
[ -128,-1] |
Char |
X |
X |
unsigned char |
X |
256+x |
The complement expression of C language and the conversion of unsigned and signed