的的int64_t宽度,是它总是64位? [英] Width of int64_t, is it always 64 bits?
问题描述
有关以下code
static inline float fix2float(int64_t f)
{
return (float)f / (1 << 60); // <-- error here
}
编译器是给我这些警告。
The compiler is giving me these warnings.
warning: left shift count >= width of type
warning: division by zero
为什么编译器提供这些警告时64> 60
Why is the compiler giving these warnings when 64 > 60?
推荐答案
1
是不是在你的C执行64位数字。这是一个 INT
,这很可能是32位。
1
is not a 64-bit number in your C implementation. It is an int
, which is likely 32 bits.
编译器不过目一名前pression,看到有一个的int64_t
参与,因此其它算术应使用64位。从他们的部分建立了前pressions。在部分(1 LT;&LT; 60)
,编译器可以识别之一,并赋予它一个类型 INT
中,因为这是的C规则说做简单的常量的值(有十六进制表示法,后缀和较大值额外的规则)。因此, 1 LT;&LT; 60
试图通过60位转移的 INT
。因为 INT
您的系统上只有32位,编译器会发出警告。
The compiler does not look over an expression and see that there is an int64_t
involved, therefore other arithmetic should use 64 bits. It builds up expressions from their parts. In the part (1 << 60)
, the compiler recognizes one and gives it a type of int
, because that is what the C rules say to do with simple constant values (there are additional rules for hexadecimal notation, suffixes, and large values). So, 1 << 60
tries to shift an int
by 60 bits. Since the int
on your system is only 32 bits, the compiler warns you.
有一个更好的方式来写,这是返回F * 0x1p-60F;
。 0x1p-60F
是浮动
常量值为2 -60 。
A better way to write this is return f * 0x1p-60f;
. 0x1p-60f
is a float
constant with value 2–60.
这篇关于的的int64_t宽度,是它总是64位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!