gpt4 book ai didi

c - C中按位运算的返回类型

转载 作者:太空狗 更新时间:2023-10-29 17:04:19 26 4
gpt4 key购买 nike

我有一个 int (32) 值 Val 打包成

----------------------------------------
| 32-XXXXXXXXX-16 | 15-short-int-val-0 |
----------------------------------------

当我从这个 Val 中提取短整数值时,如果我执行操作

short temp = Val & 0x0000FFFF;

Val & 0x0000FFFF 操作的返回类型是什么?我是否需要将值强制转换为 (short)(Val & 0x0000FFFFF) 才能将正确的数据存储在 temp 中?

问题出现了,因为我假设十六进制数字本质上被视为无符号整数。

上面的操作和做的有什么不同

short temp = Val & 0xFFFF;

最佳答案

让我们假设 32-bit int , 16-bit short8-bit char .

short temp = Val & 0x0000FFFF;

Val类型为 int . 0x0000FFFF0xFFFF 相同类型为 int

表达式Val & 0x0000FFFF那么也是 int 类型的并隐式转换为 shorttemp 的初始化时对象。

所以:

What is will be the return type of the operation Val & 0x0000FFFF ?

参见上文,int .

Do I need to type cast the value to (short)(Val & 0x0000FFFFF) to have correct data stored in temp?

见上文,否,因为表达式被隐式转换为 shorttemp 的初始化时对象。

The doubt arises since I assume hex numbers are inherently treated as unsigned integers.

这是一个错误的假设 0x0000FFFF是签名类型 int .

关于c - C中按位运算的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17807442/

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