Unintended sign extension
Posted cleverlzc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unintended sign extension相关的知识,希望对你有一定的参考价值。
符号扩展包括:
-
Integral promotion
A
char
,short
, enumerated type, or bit-field, whether signed or unsigned, can be used in any expression that calls for anint
. If anint
can hold all possible values of the original type, the value is converted to anint
. Otherwise, it is converted to anunsigned int
. -
Conversion between signed and unsigned integers
When a negative signed integer is promoted to an unsigned integer of the same or larger type, it is first promoted to the signed equivalent of the larger type, then converted to the unsigned value.
修复方法:强转
To fix unintended sign extension problems, you must use explicit casting to achieve the intended results.
关于“无意的符号扩展”,这里有篇文章讲的很详细
http://docs.oracle.com/cd/E19253-01/816-5138/convert-2/index.html
以上是关于Unintended sign extension的主要内容,如果未能解决你的问题,请参考以下文章