1

double long在我的 64 位计算机中使用并且sizeof(double long)16 bytes. 所以我做了以下任务:

  long double f = 0xA0000000000000000000000000000000; // 32 characters in hex

我得到这个警告:warning: integer constant is too large for its type。该警告似乎不正确,我的常量可以轻松保存为 128 位值。那么,为什么我会收到警告?

我也有这个代码:

  long double tmp = 0x90000000CCCCCCCC0000000000000000;
  long double res = (f & tmp);  // where f is the previously defined variable

我收到以下错误error: invalid operands to binary &。为什么 ?

4

3 回答 3

2
error: invalid operands to binary &

because f and tmp are floating point numbers, not integers, they cannot serve as argument to binary &.

long double f = 0xA0000000000000000000000000000000

You are initializing a floating-point variable with an integer constant. That is OK per se, but the integer constant has its own type, which can only be as large as the largest integer type you have (the compiler chooses the type that fits). If the largest integer type on your compiler has less than 128 bits, this won't work.

BTW that sizeof(double long) == 16 doesn't mean that long double has 128 bit representation.

于 2012-02-13T11:35:52.940 回答
1

0xA0000000000000000000000000000000是一个整数常量,根据您的系统,它可能不受支持。整数常量的较大类型是unsigned long long,如果系统中的宽度unsigned long long为 64 位,则此常量不适合。

您可以做的是使用十六进制浮点常量而不是十六进制整数常量。

long double f = 0xA0000000000000000000000000000000p0L;
                                                  ^^ the exponent is 0

没关系。

p引入指数(必需)并指示L它是long double文字(默认为double)。

十六进制浮点常数是 C99 加法,请注意,在十六进制浮点常数中,指数部分是必需的。

关于按位运算符(问题的第二部分),按位运算符的操作数必须是整数类型,因此您不能对浮点值执行按位运算。

于 2012-02-13T13:00:29.530 回答
1

扩展我的评论:

long double是浮点类型。这就是为什么您不能对它们执行按位运算符的原因。

不幸的是,该标准不保证 128 位整数类型。我所知道的唯一真正支持它作为扩展的编译器是GCC via __int128。(编辑:它看起来并不总是支持它。)

MSVC 也有这种__int128类型,但它只是保留和未实现。

如果您只进行按位运算,则可以使用具有两个 64 位整数的结构。或者,如果您使用的是 x86,则可以使用 128 位 SSE 数据类型。

于 2012-02-13T11:38:09.520 回答