警告:左移位数>类型=宽度 [英] warning: left shift count >= width of type
问题描述
我很新的处理比特和编译时已经就死在以下警告:
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行看起来像这样
My line 7 looks like this
unsigned long int x = 1 << 32;
这将使意义,如果长在我的系统大小为32位。然而,的sizeof(长)返回8 CHAR_BIT被定义为8暗示长应该是8×8 = 64位。
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.
我缺少的是在这里吗?是的sizeof和CHAR_BIT不准确或有我误解了一些基本的东西?
What am I missing here? Are sizeof and CHAR_BIT inaccurate or have I misunderstood something fundamental?
推荐答案
长
可能是64位的,但 1
仍然是一个 INT
。你需要让 1
A 使用
后缀:→长整型
long
may be a 64-bit type, but 1
is still an int
. You need to make 1
a long int
using the L
suffix:
unsigned long x = 1UL << 32;
(你也应该让无符号
使用 U
后缀我已经证明,以避免问题的左移一个符号整数。有没有问题,当一个长
宽64位,您可以通过32位的移位,但如果你转向63位),这将是一个问题
(You should also make it unsigned
using the U
suffix as I've shown, to avoid the issues of left shifting a signed integer. There's no problem when a long
is 64 bits wide and you shift by 32 bits, but it would be a problem if you shifted 63 bits)
这篇关于警告:左移位数&GT;类型=宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!