从铸造" INT"到"无符号短" 〜"运用位运算符&QUOT后; [英] Cast from "int" to "unsigned short" after applying bitwise operator "~"
问题描述
静态分析工具,我用提出警告此code:
UINT16 VAR1 = 1U;
UINT16 VAR2 =〜VAR1;
我检查MISRA C规则,2004年,我发现10.5规则:
如果该位运算符〜和<<被应用到操作数OD基础类型的 unsigned char型或无符号短的,其结果应立即转换为基本类型的操作数。
块引用>好吧,它不是,隐式转换应用(我觉得投是指隐性或显性CAST)的问题。但10.1规则说:
整数类型的前pression的值不应隐式转换为不同的基本类型的前pression是复杂的。
块引用>复杂操作的一个previous例子是:〜u16a
我改变我的code为:
UINT16 VAR1 = 1U;
UINT16 VAR2 =(UINT16)〜VAR1;和我得到一个警告:我觉得INT负值转换为无符号整型值并不安全。我检查C99标准( ISO C99 )第6.3.1.3但我不明白,如果转换的 INT 的到的无符号短的有明确规定。
在<一个href=\"http://embeddedgurus.com/stack-overflow/2009/08/a-tutorial-on-signed-and-unsigned-integers/\">EmbeddedGurus文章我读
C =(unsigned int类型)一个; / *由于是积极的,这是投*安全/
我的问题:
- 有从的符号int 的到的无符号短的未指定的行为显式转换?
- 如果有,如何使用补充运营商,在安全的方式无符号短?
解决方案算术运算和位运算符的运算始终经受的标准促销的计算值之前。什么比一个短
INT
晋升为INT
或unsigned int类型
,根据不同的平台(即取决于是否INT
可以重新present所宣传的类型的所有值)。在你的平台上,
uint16_t
的标准提升到INT
,因为你的INT
可以重新present的uint16_t
所有值。然后,按位否定应用到INT
价值,这是问题的原因。要得到一个确定的结果,独立于平台,值转换为
unsigned int类型
自己:uint16_t VAR2 =(uint16_t)〜((unsigned int类型)VAR1);
请注意,这是永远正确的,因为
unsigned int类型
要求是能够重新present一个uint16_t <的所有值/ code>。
Static analysis tool I use raises a warning for this code :
uint16 var1 = 1U; uint16 var2 = ~var1;
I check MISRA C 2004 rules and I find 10.5 rule :
If the bitwise operators ~ and << are applied to an operand od underlying type unsigned char or unsigned short, the result shall be immediately cast to the underlying type of the operand.
Ok, it's not a problem, implicit cast is applied (I think "cast" means implicit or explicit cast). But 10.1 rule says :
The value of an expression of integer type shall not be implicitly converted to a different underlying type the expression is complex.
An previous example of complex operation are : ~u16a
I change my code to :
uint16 var1 = 1U; uint16 var2 = (uint16) ~var1;
And I obtain another warning : I think conversion of int negative value to unsigned int value not safe. I check C99 standard (ISO C99) § 6.3.1.3 but I don't understand if conversion of int to unsigned short are clearly specified.
In EmbeddedGurus article I read :
c = (unsigned int) a; /* Since a is positive, this cast is safe */
My questions :
- Have explicit conversion from signed int to unsigned short unspecified behavior ?
- If yes, how to use complement operator with unsigned short in safe way ?
解决方案The operands of the arithmetic and bitwise operators always undergo the standard promotions before the value is computed. Anything shorter than an
int
is promoted to eitherint
orunsigned int
, depending on the platform (i.e. depending on whetherint
can represent all values of the type that's being promoted).On your platform,
uint16_t
is standard-promoted toint
, since yourint
can represent all values of auint16_t
. Then the bitwise negation is applied to thatint
value, which is the cause of the problem.To get a deterministic result independent of the platform, convert the value to an
unsigned int
yourself:uint16_t var2 = (uint16_t) ~((unsigned int) var1);
Note that this is always correct, since
unsigned int
is required to be able to represent all the values of auint16_t
.这篇关于从铸造&QUOT; INT&QUOT;到&QUOT;无符号短&QUOT; 〜&QUOT;运用位运算符&QUOT后;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!