是(uint64_t)-1保证产生0xffffffffffffffff? [英] Is (uint64_t)-1 guaranteed to yield 0xffffffffffffffff?

查看:2272
本文介绍了是(uint64_t)-1保证产生0xffffffffffffffff?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,它是由C标准明确定义(unsigned)-1 必须产生2 ^ n-1,i。 e。其所有位都设置的无符号整数。对于(uint64_t)-1ll 也是如此。但是,我找不到C11标准中指定如何解释(uint64_t)-1 的内容。

I know, that it is well defined by the C standard that (unsigned)-1 must yield 2^n-1, i. e. an unsigned integer with all its bits set. The same goes for (uint64_t)-1ll. However, I cannot find something in the C11 standard that specifies how (uint64_t)-1 is interpreted.

,问题是:在C标准中有什么保证,下列哪项是真的?

So, the question is: Is there any guarantee in the C standard, which of the following holds true?

(uint64_t)-1 == (uint64_t)(unsigned)-1   //0x00000000ffffffff
(uint64_t)-1 == (uint64_t)(int64_t)-1    //0xffffffffffffffff


推荐答案

是的。参见C11 6.3.1.3有符号和无符号整数:

Yes. See C11 6.3.1.3 Signed and unsigned integers:


1当整数类型的值转换为除_Bool之外的另一种整数类型时,if该值可以由新类型表示,不变。

1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

2否则,如果新类型为无符号,则通过重复添加或减去一个值 60)

2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.60)

3否则,可以在新类型中表示的最大值。 ,新类型被签名,并且该值不能在其中表示;

3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

60)规则描述了数学值的算术运算,而不是

60) The rules describe arithmetic on the mathematical value, not the value of a given type of expression.

情况2适用,因此-1减去模数0x10000000000000000得到0xffffffffffffffff。

Case 2 applies, so -1 is reduced modulo 0x10000000000000000 to yield 0xffffffffffffffff.

这篇关于是(uint64_t)-1保证产生0xffffffffffffffff?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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