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

发布于 2024-10-02 11:27:07 字数 428 浏览 4 评论 0原文

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

 7:警告:左移计数 >= 类型宽度

我的第 7 行看起来像这样

unsigned long int x = 1 << 32;

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

我在这里缺少什么? sizeofCHAR_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别把无礼当个性 2024-10-09 11:27:07

long 可能是 64 位类型,但 1 仍然是 int。您需要使用 L 后缀将 1 设为 long int:(

unsigned long x = 1UL << 32;

您还应该使用正如我所展示的 U 后缀,以避免左移有符号整数的问题当 long 是 64 位宽并且移位 32 位时没有问题,但如果你移动 63 位就会有问题)

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;

(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)

甜宝宝 2024-10-09 11:27:07

unsigned long 是 32 位或 64 位,这取决于您的系统。 unsigned long long 始终为 64 位。您应该按如下方式进行操作:

unsigned long long x = 1ULL << 32

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:

unsigned long long x = 1ULL << 32
一抹微笑 2024-10-09 11:27:07

所接受的解决方案对于[常数]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.

心凉怎暖 2024-10-09 11:27:07

无符号长 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.

林空鹿饮溪 2024-10-09 11:27:07

您无法将值移至其最大位

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

因此,这会生成警告

left shift count >= width of type (ie type = int = 32)

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 )

瘫痪情歌 2024-10-09 11:27:07

你可以使用类似的东西:

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

You can use something like that:

unsigned long x = 1;
x = x << 32;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文