【发布时间】:2019-07-23 10:16:32
【问题描述】:
我一直在使用一些代码来确保符合 MISRA。这段代码的问题是
Operands shall not be of an inappropriate essential type. The operand of the ? operator is of an inappropriate essential type category unsigned.
我认为问题在于第一个参数是无符号数据类型,而不是布尔值,这意味着下面的修复将起作用。
原作,
return (uint32Var & 0x80000000u) ? 0u : 1u;
我对代码的更改,
return ( (uint32Var & 0x80000000u)!=0u ) ? 0u : 1u;
这是一个正确的改变吗?我担心更改代码的功能,但据我所知,至少在 if 逻辑中,操作数在 if ( numVar ) 运算符内被评估为 numVar != 0。
【问题讨论】:
-
您可以完全避免使用三元运算符,例如
return 0 == (uint32Var & 0x80000000u);。 (我不知道这是否符合 MISRA(ble) 规则。)
标签: c logic bitwise-operators conditional-operator misra