在C ++中,1和1i64有什么区别? [英] In C++, what is the difference between 1 and 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) {
这就是我遵循微软的建议:
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位。 j
和 0
将隐式转换为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屋!