有符号与无符号整数例子(注意,测试平台为ubuntu 14.04 32位,gcc 4.8)
#include<iostream>
usingnamespace std;
int main()
{
int n =0;
n--;
unsignedint u =(unsignedint)n;
unsignedlonglongint v =(unsignedlonglongint)n;
cout <<"Unsigned int value for "<< n <<" is "<< u <<"(0x"
<< hex << u <<")"<< endl;
cout <<"0x"<< u <<" increase 1 is "<<(u+1)<< endl;
cout <<"Unsigned long long int value for "<< dec << n <<" is "<< v <<"(0x"
<< hex << v <<")"<< endl;
cout <<"0x"<< v <<" increase 1 is "<<(v+1)<< endl;
}
输出结果如下:
Unsignedint value for-1is4294967295(0xffffffff)
0xffffffff increase 1is0
Unsignedlonglongint value for-1is18446744073709551615(0xffffffffffffffff)
0xffffffffffffffff increase 1is0