gcc 4.1.2:错误:整数常量对于“long”来说太大;类型

发布于 2024-12-23 02:57:40 字数 416 浏览 3 评论 0原文

我编译了一段关于哈希函数的代码并得到了错误:整数常量对于“long”类型来说太大了。我用谷歌搜索了一下,它说要添加后缀“ULL”,但我确实有 ULL 作为后缀。这个后缀仅受 gcc 4.4.1 支持,我的机器上只有 gcc 4.1.2,并且不允许我安装新的编译器。有什么办法可以更改代码来解决问题吗?

谢谢, -托尼

unsigned long long hash(string k){ //FNV hash
   unsigned long long x = 14695981039346656037ULL;
   for (unsigned int y=0;y<k.length();y++){
      x = x ^ (k[y]);
      x = x * 1099511628211;
   }
   return (x);
}

I compiled a piece of code about hash function and got error: integer constant is too large for ‘long’ type. I did Google it and it said to add the suffix "ULL", but I did have ULL as suffix. This suffix is supported only by gcc 4.4.1 and I only have gcc 4.1.2 on the machine and I'm not allowed to install a new compiler. Is there any way to change the code so fix the problem?

Thanks,
-Tony

unsigned long long hash(string k){ //FNV hash
   unsigned long long x = 14695981039346656037ULL;
   for (unsigned int y=0;y<k.length();y++){
      x = x ^ (k[y]);
      x = x * 1099511628211;
   }
   return (x);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

任性一次 2024-12-30 02:57:40

1099511628211 对于(32 位)long 来说也太大了;也添加 ULL 后缀。

AFAIK,GCC 4.x 支持所有 xlong long。事实上,我说过 GCC 3.x 支持 long long,至少对于更新的 x 值是这样。

在 MacOS X 10.7.2 上以 32 位或 64 位模式使用 G++ 4.6.1 时,我很难让您的代码触发任何警告。但是,我可以从 XCode 4.x 的 LLVM 编译器得到投诉(g++ --version 输出以 i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2 开头。 1(基于 Apple Inc. build 5658)(LLVM build 2335.15.00))确实会抱怨,直到我添加第二个ULL,但前提是我在 32 位模式下编译。如果在 64 位模式下使用它,它也不会抱怨。

1099511628211 is also too big for a (32-bit) long; add the ULL suffix there, too.

AFAIK, GCC 4.x supports long long for all x. Indeed, I'd have said that GCC 3.x supported long long, at least for the more recent values of x.

I'm having difficulty making your code trigger any warning, using G++ 4.6.1 on MacOS X 10.7.2 in either 32-bit or 64-bit mode. However, I can get the complaint from the LLVM compiler from XCode 4.x (g++ --version output starts with i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)) does complain until I add the second ULL, but only if I compile in 32-bit mode. If that is used in 64-bit mode, it doesn't complain either.

古镇旧梦 2024-12-30 02:57:40

您可以编写一个计算结果为相同数字的常量表达式:

unsigned long long x = 1469598103UL*10000000000+9346656037UL;

这使用 g++ 4.2.1 进行编译

You can write a constant expression that evaluates to the same number:

unsigned long long x = 1469598103UL*10000000000+9346656037UL;

This compiles with g++ 4.2.1

2024-12-30 02:57:40

试试这个:

#define HIGHER_BITS 0xcbf29ce4
#define LOWER_BITS 0x84222325

unsigned long long x = (HIGHER_BITS << 32) | LOWER_BITS;

这两个数字是使用以下示例程序获得的:

#include <stdio.h>
#include <limits.h>

int main(int argc, char **argv)
{
        unsigned long long x = 14695981039346656037ULL;
        printf("0x%08llx, 0x%08llx\n", x >> 32, x & UINT_MAX);
}

Try this:

#define HIGHER_BITS 0xcbf29ce4
#define LOWER_BITS 0x84222325

unsigned long long x = (HIGHER_BITS << 32) | LOWER_BITS;

These two numbers were obtained using this sample program:

#include <stdio.h>
#include <limits.h>

int main(int argc, char **argv)
{
        unsigned long long x = 14695981039346656037ULL;
        printf("0x%08llx, 0x%08llx\n", x >> 32, x & UINT_MAX);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文