我有一个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;
解决方法
@H_404_19@ 我们假设32位int,16位short和8位char.short temp = Val & 0x0000FFFF;
Val的类型为int.与0xFFFF相同的0x0000FFFF也是int类型.
表达式Val& 0x0000FFFF也是int类型,并在初始化temp对象时隐式转换为short.
所以:
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?
请参见上文,因为表达式在临时对象初始化时被隐式转换为short.
The doubt arises since I assume hex numbers are inherently treated as unsigned integers.
这是一个错误的假设,因为这里0x0000FFFF是有符号类型的int.