gpt4 book ai didi

从 unsigned long long 转换为 unsigned int

转载 作者:行者123 更新时间:2023-12-01 12:26:38 40 4
gpt4 key购买 nike

我四处寻找,试图找到这个问题的答案,但我找不到明确的答案。
我看到了 this回答他在旁注中写道:

The rules are different for unsigned types; the result of converting a signed or unsigned integer to an unsigned type is well defined.



那么什么是明确定义的行为?
unsigned long long 转换时至 unsigned int有明确的行为吗?它只是切断了 32 个 MSB 吗? (给我留下 32 个 LSB)。
让我们假设 sizeof(unsigned int)4sizeof(unsigned long long)8在我的系统中。
分配时不进行强制转换和分配时进行强制转换是否有不同的行为?

最佳答案

标准说:

6.3.1.3 Signed and unsigned integers
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. 49)
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.



第 2 点描述了模运算,它与在这种情况下切断 MSB 具有相同的效果。 (当新类型的最大值加一是数基的幂时,效果相同。)

如果您使用强制转换,则没有区别。

关于从 unsigned long long 转换为 unsigned int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940780/

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