C语言 警告:左移计数 >= 类型宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4201301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:05:37  来源:igfitidea点击:

warning: left shift count >= width of type

cbit-manipulationlong-integerbit-shift

提问by Rupert Madden-Abbott

I'm very new to dealing with bits and have got stuck on the following warning when compiling:

我对处理位很陌生,并且在编译时遇到了以下警告:

 7: warning: left shift count >= width of type
 7: warning: left shift count >= width of type

My line 7 looks like this

我的第 7 行看起来像这样

unsigned long int x = 1 << 32;

This would make sense if the size of longon my system was 32 bits. However, sizeof(long)returns 8and CHAR_BITis defined as 8suggesting that long should be 8x8 = 64 bits long.

如果long我的系统上的大小为 32 位,这将是有意义的。但是,sizeof(long)返回8并被CHAR_BIT定义为8建议 long 应该是 8x8 = 64 位长。

What am I missing here? Are sizeofand CHAR_BITinaccurate or have I misunderstood something fundamental?

我在这里缺少什么?是sizeofCHAR_BIT不准确的或有我误解一些基本的东西?

回答by James McNellis

longmay be a 64-bit type, but 1is still an int. You need to make 1a long intusing the Lsuffix:

long可能是 64 位类型,但1仍然是int. 你需要做1一个long int使用L后缀:

unsigned long x = 1UL << 32;

(You should also make it unsignedusing the Usuffix as I've shown, to avoid the issues of left shifting a signed integer. There's no problem when a longis 64 bits wide and you shift by 32 bits, but it would be a problem if you shifted 63 bits)

(您还应该unsigned使用U我显示的后缀来制作它,以避免左移有符号整数的问题。当 along为 64 位宽并且您移动 32 位时没有问题,但是如果您移位 63 位)

回答by tznza

unsigned longis 32 bit or 64 bit which depends on your system. unsigned long longis always 64 bit. You should do it as follows:

unsigned long是 32 位还是 64 位,这取决于您的系统。unsigned long long总是 64 位。你应该这样做:

unsigned long long x = 1ULL << 32

回答by Muthuraman

unsigned long x = 1UL << 31;

无符号长 x = 1UL << 31;

Not show the error message. Because before you specify the 32, is not true because only limited to 0-31.

不显示错误信息。因为之前你指定了 32,是不正确的,因为只限于 0-31。

回答by vipul bagga

You can't shift a value to its max bit

你不能将一个值移到它的最大位

int x;         // let int be 4 bytes so max bits : 32 
x <<= 32; 

So, this generates the warning

所以,这会产生警告

left shift count >= width of type (i.e type = int = 32 )

left shift count >= width of type (i.e type = int = 32 )

回答by VectorVictor

The accepted solution is fine for [constant]ULL<<32 but no good for existing variables - e.g. [variable]<<32. The complete solution for variables is: ((unsigned long long)[variable]<<32). Aside: My personal opinion of this warning is that it is totally unnecessary in the first place. The compiler can see what the receiving data type is and knows the width of the parameters from the definitions in headers or constant values. I believe Apple could make the clang compiler a little more intelligent than it is regarding this warning.

接受的解决方案适用于 [constant]ULL<<32,但对现有变量没有好处 - 例如 [variable]<<32。变量的完整解决方案是:((unsigned long long)[variable]<<32)。旁白:我个人对这个警告的看法是,它首先是完全没有必要的。编译器可以看到接收的数据类型是什么,并从头文件或常量值的定义中知道参数的宽度。我相信 Apple 可以让 clang 编译器比关于这个警告更智能一点。

回答by John

You can use something like that:

你可以使用这样的东西:

unsigned long x = 1;
x = x << 32;