用C分配128位的整数 [英] Assigning 128 bit integer in C

查看:240
本文介绍了用C分配128位的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在GCC 4.9.1分配一个128位的整数,我得到一个警告:整型常量是其类型太大

例code

  INT主要(无效){
  __uint128_t P = 47942806932686753431;  返回0;
}

输出

我与的gcc -std = C11 -o测试test.c的编译和获取:

  test.c的:在函数'主':
test.c的:2:19:警告:整型常量是其类型太大
   __uint128_t P = 47942806932686753431;
               ^

我做得不对,或这是gcc的一个bug?


解决方案

  

我做得不对,或这是gcc的一个bug?


问题出在 47942806932686753431 部分,而不是在 __ uint128_t P 。据 GCC文档没有办法申报128位的常数:


  

有在GCC不支持对前pressing型__int128的整型常量与长长整型宽度小于128位的目标。


所以,看来,尽管你可以有128位的变量的,你不能有128位的常量的,除非你的长长为128位宽。

解决方法可以是使用基本的算术运算,从窄的整型常量建设128位的值,并希望编译器执行不变折叠

When I try to assign an 128 bit integer in gcc 4.9.1, I get a warning: integer constant is too large for its type.

Example Code

int main(void) {
  __uint128_t p = 47942806932686753431;

  return 0;
}

Output

I'm compiling with gcc -std=c11 -o test test.c and I get:

test.c: In function ‘main’:
test.c:2:19: warning: integer constant is too large for its type
   __uint128_t p = 47942806932686753431;
               ^

Am I doing something wrong or is this a bug in gcc?

解决方案

Am I doing something wrong or is this a bug in gcc?

The problem is in 47942806932686753431 part, not in __uint128_t p. According to gcc docs there's no way to declare 128 bit constant:

There is no support in GCC for expressing an integer constant of type __int128 for targets with long long integer less than 128 bits wide.

So, it seems that while you can have 128 bit variables, you cannot have 128 bit constants, unless your long long is 128 bit wide.

The workaround could be to construct 128 bit value from "narrower" integral constants using basic arithmetic operations, and hope for compiler to perform constant folding.

这篇关于用C分配128位的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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