gpt4 book ai didi

C型铸件: bitwise or arithmetic

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:48 24 4
gpt4 key购买 nike

我想知道类型转换在实践中是如何在类型之间进行的(在“嵌入式​​”C 中)。例如:如果我有一个 Signed 16-bit 数字,值为 -80d11010000b,我想将它转换为 无符号 16 位。我得到哪个值,是 80d 还是 208。

我的意思是,转换是按位转换还是算术转换?只是位的解释改变了,还是转换真的改变了位?

如果它真的改变了位,我将如何以仅解释改变的方式来做。例如,当我从 i2c 设备读取 u8 值并且必须将这些位解释为一个有符号的值。

最后,实际上与上面相同:如果它不更改位,我将如何转换它以使其更改位而不是值?

亲切的问候,杰西

最佳答案

无论何时进行强制转换,都会触发类型转换,这是由 C11 6.3.1.3 指定的:

6.3.1.3 Signed and unsigned integers

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.

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)

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.

第二段介绍了从有符号转换为无符号的过程。它以这种奇怪的方式编写,涵盖了所有形式的签名格式。在实践中,在 16 位二进制补码系统中,从 -80d(十六进制 FFB0h)开始,您最终得到值 FFB0h ,这是无符号数 65456d

它不会改变任何位。

关于C型铸件: bitwise or arithmetic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47225301/

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