警告:左移计数 >= 类型宽度
我对处理位非常陌生,并且在编译时遇到以下警告:
7:警告:左移计数 >= 类型宽度
我的第 7 行看起来像这样
unsigned long int x = 1 << 32;
如果我的系统上的 long 大小是 32 位,这将是有意义的。但是,sizeof(long)
返回 8
,并且 CHAR_BIT
定义为 8
,表明 long 应为 8x8 = 64位长。
我在这里缺少什么? sizeof
和 CHAR_BIT
是否不准确,或者我是否误解了一些基本的东西?
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
My line 7 looks like this
unsigned long int x = 1 << 32;
This would make sense if the size of long
on my system was 32 bits. However, sizeof(long)
returns 8
and CHAR_BIT
is defined as 8
suggesting that long should be 8x8 = 64 bits long.
What am I missing here? Are sizeof
and CHAR_BIT
inaccurate or have I misunderstood something fundamental?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
long
可能是 64 位类型,但1
仍然是int
。您需要使用L
后缀将1
设为long int
:(您还应该使用正如我所展示的
U
后缀,以避免左移有符号整数的问题当long
是 64 位宽并且移位 32 位时没有问题,但如果你移动 63 位就会有问题)long
may be a 64-bit type, but1
is still anint
. You need to make1
along int
using theL
suffix:(You should also make it
unsigned
using theU
suffix as I've shown, to avoid the issues of left shifting a signed integer. There's no problem when along
is 64 bits wide and you shift by 32 bits, but it would be a problem if you shifted 63 bits)unsigned long
是 32 位或 64 位,这取决于您的系统。unsigned long long
始终为 64 位。您应该按如下方式进行操作:unsigned long
is 32 bit or 64 bit which depends on your system.unsigned long long
is always 64 bit. You should do it as follows:所接受的解决方案对于[常数]ULL<<32 来说很好,但对于现有变量则不好 - 例如[变量]<<32。变量的完整解决方案是:
((无符号长长)[变量]<<32)。旁白:我个人对这个警告的看法是,它首先是完全没有必要的。编译器可以看到接收的数据类型是什么,并从标头或常量值的定义中知道参数的宽度。我相信苹果可以让 clang 编译器比这个警告更智能一些。
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.
无符号长 x = 1UL << 31;
不显示错误信息。因为之前指定了32,是不正确的,因为只限于0-31。
unsigned long x = 1UL << 31;
Not show the error message. Because before you specify the 32, is not true because only limited to 0-31.
您无法将值移至其最大位
因此,这会生成警告
left shift count >= width of type (ie type = int = 32)
You can't shift a value to its max bit
So, this generates the warning
left shift count >= width of type (i.e type = int = 32 )
你可以使用类似的东西:
You can use something like that: