【发布时间】:2014-06-09 10:06:00
【问题描述】:
我想在我的程序中使用以下代码,但 gcc 不允许我将 1 左移超过 31。
sizeof(long int) 显示 8,那是不是意味着我可以左移到 63?
#include <iostream>
using namespace std;
int main(){
long int x;
x=(~0 & ~(1<<63));
cout<<x<<endl;
return 0;
}
编译输出如下警告:
left shift `count >= width` of type [enabled by default] `x=(~0 & ~(1<<63))`;
^
输出为-1。如果我左移 31 位,我会得到 2147483647,正如预期的 int。
我希望除了 MSB 之外的所有位都会打开,从而显示数据类型可以容纳的最大值。
【问题讨论】:
-
#include <limits>和std::numeric_limits<long int>::max()?您假设您知道实现如何存储有符号整数。那不是便携式的。 char 也不能保证是 8 位。 -
@tim-seguine:被低估的评论。