简体   繁体   English

C中的三元运算符参数评估

[英]Ternary operator argument evaluation in C

I've been working with some code to ensure MISRA compliance.我一直在使用一些代码来确保 MISRA 合规性。 The issue with this piece of code is这段代码的问题是

Operands shall not be of an inappropriate essential type. The operand of the ? operator is of an inappropriate essential type category unsigned. 

I assume the issue is with the first argument being an unsigned data type, instead of boolean, which means the fix bellow would work.我认为问题在于第一个参数是无符号数据类型,而不是布尔值,这意味着下面的修复将起作用。

The original,原本的,

 return (uint32Var & 0x80000000u) ? 0u : 1u;

My change to the code,我对代码的更改,

 return ( (uint32Var & 0x80000000u)!=0u ) ? 0u : 1u;

Is this a correct change to make?这是一个正确的改变吗? I'm concerned about changing the functionality of the code but as far as I know, at least in if logic, the operand is evaluated as numVar != 0 inside the if ( numVar ) operator.我担心更改代码的功能,但据我所知,至少在 if 逻辑中,操作数在if ( numVar )运算符中被评估为numVar != 0

That is safe.那是安全的。

You'll be comparing the unsigned 32-bit:您将比较无符号 32 位:

(uint32Var & 0x80000000u)

To the unsigned int :unsigned int

0u

Usual arithmetic conversions apply to ensure that regardless of the actual types involved here, you'll be comparing values of types that are at least large enough to contains unsigned 32-bit. 通常的算术转换适用于确保无论此处涉及的实际类型如何,您都将比较至少足够大以包含无符号 32 位的类型的值。

The value (uint32Var & 0x80000000u) is a false one if it is equal to 0 , otherwise it is a true one.如果值(uint32Var & 0x80000000u)等于0则为假一,否则为真。 Comparing the value to 0 has the effect, and yielding 0 if the comparison is equal, otherwise yielding 1 is equivalent behavior.将值与0进行比较会产生影响,如果比较相等则产生0 ,否则产生1是等效行为。


As another note, the value that you end up using for the first operand of the ternary operator isn't a bool , it's an int .另请注意,您最终用于三元运算符的第一个操作数的值不是bool ,而是int The != operator yields an int value of either 0 or 1 . !=运算符产生01int值。

I assume the issue is with the first argument being an unsigned data type, instead of boolean, which means the fix bellow would work.我认为问题在于第一个参数是无符号数据类型,而不是布尔值,这意味着下面的修复将起作用。 /--/ /--/

Is this a correct change to make?这是一个正确的改变吗?

Yes.是的。 As in, it will make the code MISRA compliant.如在,它将使代码符合 MISRA。 The warning is indeed about using a non-boolean type for the 1st operand.警告确实是关于对第一个操作数使用非布尔类型。

However, assuming that the function returns uint32_t , you may as well just write:但是,假设该函数返回uint32_t ,您也可以这样写:

return (uint32_t) !(uint32Var & mask);

( mask since MISRA, like everyone else, discourages the use of "magic numbers".) mask ,因为MISRA,和其他人一样,不鼓励使用“幻数”的。)

Equivalent and also MISRA-C compatible:等效且兼容 MISRA-C:

return ~uint32Var >> bits; // where bits in this case is 31

Normally the ~ operator is a nasty piece when writing MISRA compilant code, but it is perfectly safe when you use an unsigned integer type which is not a small integer type, such as uint32_t .通常,在编写 MISRA 兼容代码时~运算符是一个令人讨厌的部分,但是当您使用无符号整数类型(不是小整数类型)时,它是完全安全的,例如uint32_t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM