gpt4 book ai didi

c++ - (uint64_t)-1 是否保证产生 0xffffffffffffffff?

转载 作者:IT老高 更新时间:2023-10-28 22:28:10 28 4
gpt4 key购买 nike

我知道,C 标准很好地定义了 (unsigned)-1 必须产生 2^n-1,即。 e.一个无符号整数,其所有位都已设置。 (uint64_t)-1ll 也是如此。但是,我在 C11 标准中找不到指定如何解释 (uint64_t)-1 的内容。

那么,问题是:C 标准中是否有任何保证,以下哪项成立?

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

最佳答案

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

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 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 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) The rules describe arithmetic on the mathematical value, not the value of a given type of expression.

情况 2 适用,因此 -1 以 0x10000000000000000 为模减少为 0xffffffffffffffff。

关于c++ - (uint64_t)-1 是否保证产生 0xffffffffffffffff?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18303682/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com