【发布时间】:2011-10-25 04:51:45
【问题描述】:
谁能解释一下为什么下面的代码会输出它的作用:
char c = -1;
cout << (c << 8) << endl;
cout << ((unsigned char) c << 8) << endl;
cout << (c << 24) << endl;
cout << ((unsigned char) c << 24) << endl;
输出:
-256
65280
-16777216
-16777216
我认为转换为 unsigned char 只会改变位的解释方式。但是,左移 8 时,结果却发生了变化。奇怪的是,左移 24 时,结果似乎并非如此。
【问题讨论】:
标签: bit-manipulation unsigned signed