在 C++ 中,1 和 1i64 有什么区别? [英] In C++, what is the difference between 1 and 1i64?

查看:39
本文介绍了在 C++ 中,1 和 1i64 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些 32 位兼容代码转换为 64 位 - 我遇到了障碍.我正在编译一个 VS2008 x64 项目,但收到此警告:

I'm converting some 32-bit compatible code into 64-bit - and I've hit a snag. I'm compiling a VS2008 x64 project, and I receive this warning:

warning C4334: '<<' : result of 32-bit shift implicitly converted to 64 bits
(was 64-bit shift intended?)

这是原始代码行:

if ((j & (1 << k)) != 0) {

如果我遵循 Microsoft 的建议,结果如下:

And here's what it looks like if I follow Microsoft's advice:

if ((j & (1i64 << k)) != 0) {

这样做是否安全,何时将在 32 位和 64 位系统上编译代码?如果是这样,请解释为什么我必须在末尾添加i64",以及为什么这不会影响 32 位编译.否则,将不胜感激.

Is this safe to do, when the code will be compiled on both 32-bit and 64-bit systems? If so, please explain why I must add "i64" to the end, and why this will not affect a 32-bit compilation. Otherwise, a work-around would be much appreciated.

除此之外,我还有一些看起来更棘手的代码.

Beyond this, I have what looks like an even trickier bit of code.

if (id[j] != id[j ^ (1u << k)]) {

我知道u"表示该数字是无符号的,但是在不超过有符号最大值的值上指定该数字有什么意义...我猜这与位移?

I understand that the "u" means that the digit is unsigned, but what's the point in specifying that on a value that doesn't exceed the signed maximum value... I'm guessing this has something to do with the bit shift?

推荐答案

1 根据 C++ 标准具有 int 类型.在 64 位 Microsoft 编译器上,int 的 sizeof = 4 个字节,这意味着 int 是 32 位变量.1i64 的类型为 __int64.

1 has type int according to C++ Standard. On 64-bit Microsoft compiler int has sizeof = 4 bytes, it means that int is 32-bit variable. 1i64 has type __int64.

当您使用移位运算符时,结果的类型与左操作数的类型相同.这意味着移动 1 你会得到 32 位的结果.Microsoft 编译器假定它可能不是您所期望的(在 64 位平台上)并给您警告消息.

When you use shift operator the type of the result is the same as the type of the left operand. It means that shifting 1 you'll get 32-bit result. Microsoft compiler assumes that it could be not what you are expecting (on 64-bit platform) and gives you warning message.

当您使用 1i64 时,两个平台上的结果都是 64 位.j0 将被隐式转换为 64 位.所以整个表达式将在 64 位变量中计算,结果将是 bool.

When you use 1i64 result will be 64-bit on both platforms. j and 0 will be implicitly casted to 64-bit. So the whole expression will be calculated in 64-bit variables and result will be bool.

因此使用 1i64 在两个 (32/64) 平台上都是安全的.

So using 1i64 is safe on both (32/64) platforms.

这篇关于在 C++ 中,1 和 1i64 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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