如何在uint64_t中转移> = 32位? [英] How to shift >= 32 bits in uint64_t?

查看:126
本文介绍了如何在uint64_t中转移> = 32位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码触发gcc警告(gcc 4.2.1):

The following code triggers a gcc warning (gcc 4.2.1):

#include <boost/cstdint.hpp>
boost::uint64_t x = 1 << 32; // warning: left shift count >= width of type

因为类型有64位,这应该很好吗?

Shouldn't it be fine since the type has 64 bits?

推荐答案

如何在uint64_t中移位> = 32位?

How to shift >= 32 bits in uint64_t?

如果您的编译器支持long long:

boost::uint64_t x = 1LL << 32;

否则:

boost::uint64_t x = boost::uint64_t(1) << 32;

那不是很好,因为该类型具有64位吗?

不.即使x是64位,但1不是. 1是32位.结果的使用方式对结果的生成方式没有影响.

No. Even though x is 64 bits, 1 isn't. 1 is 32 bits. How you use a result has no effect on how that result is generated.

这篇关于如何在uint64_t中转移&gt; = 32位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆