Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
First, describe the definitions of the two extensions
Turn http://blog.csdn.net/jaylong35/article/details/6160736
Symbol extension: When storing a signed number with more memory, since the sign bit is in the first bit of the number, after the expansion, the sign bit still needs to be in the first place, so when you extend a negative, you need to fully assign the extension to 1. For positive numbers, the symbol extension is the same as the 0 extension because the sign bit is 0.
For example, a 8-bit binary representation-1, then 10000001
If the book is represented in 16-bit binary, then the 11111111 10000001 High is all 1, this is called the symbol extension, which is used primarily for its operands.
0 extension is full complement 0. No matter what the sign bit is, the high 8 bits all complement 0.
The above definition can be seen in C + +, if you convert a char to a shaping, there will be this problem
If you want a positive number, then if the ASCII value of one character is less than 0, and the direct type is cast with (int) C, the result is a negative number from the symbol extension. To get a positive number, be sure to use (int) (unsigned char) c, because unsigned char removes the sign bit of C, so after such a type conversion, the conversion with (int) is a positive number.
Examples are as follows:
[C-sharp]View PlainCopy
- #include <stdio.h>
- int main ()
- {
- int i = 129;
- Char cha= (char) i;
- int c = (int) (unsigned char) ChA;
- int b = (int) ChA;
- printf ("sign extension:%d/n", b);
- printf ("zero extension:%d/n", c);
- System ("pause");
- return 1;
- }
Operation Result:
Sign Extension:-127
Zero extension:129
Please press any key to continue ...
Note in C + +, 0 extension and sign bit extension