【问题标题】:64 bit integer bizarre results in the case of comparison64位整数比较情况下的奇异结果
【发布时间】:2012-07-29 01:27:25
【问题描述】:

我在 64 位 AIX 上使用 __int64 数据类型,在与 0 比较的情况下,我得到了奇怪的结果。

代码 sn-p:

__int64 indexExistingPart =  GetValue(); // GetValue always returns -1.

if (indexExistingPart < 0 )
{
    DoSomething(); //control never comes to this part of the code
}

我也尝试将 0 分配给另一个 __int64 变量并用于比较。但是,这也不起作用:

__int64 indexExistingPart =  GetValue(); // GetValue always returns -1.

__int64 temp =0;

if (indexExistingPart < temp )
{
    DoSomething(); //control never comes to this part of the code
}

为什么比较运算符不适用于 64 位整数?有什么解决办法吗?

【问题讨论】:

  • __int64 不是标准类型 - 它在哪里以及如何定义?另外,GetValue() 的返回类型是什么?
  • 你确定 __int64 不是无符号的吗?
  • @Paul : GetValue() 的返回类型很长。当我在 AIX 上使用 __int64 时,我没有收到任何编译错误。
  • 您还没有真正回答这个问题 - __int64 是如何以及在哪里定义的?还有什么是您系统上的long - 例如是 4 个字节还是 8 个字节?另外,您是在构建 32 位还是 64 位可执行文件?
  • __int64 是一个 Windows 特质 - 您必须在某个地方为您的 AIX 构建定义它 - 那么它是如何定义的?

标签: c++ aix


【解决方案1】:

症状与__int64 是您项目中unsigned long 的typedef 一致。 (__int64 不是 AIX 提供的类型,至少根据 IBM 文档。)试试这个:

#include <stdint.h> // or #include <sys/types.h>, or #include <inttypes.h>

int64_t indexExistingPart = GetValue();  // or "signed long indexExistingPart ..."

if (indexExistingPart < 0 )
{
    DoSomething();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2017-04-15
    • 1970-01-01
    • 2022-01-11
    • 2012-05-12
    • 1970-01-01
    相关资源
    最近更新 更多