CXX.BITOP.BOOL_OPERAND

Potential logic error: Boolean expressions should not be used directly as bitwise operator arguments. Consider using logical operators instead.

This checker reports defects when Boolean operands are used with bitwise operators.

Vulnerable code example

1   void func(bool var1, bool var2)
2   {
3     int var3 = var1 | var2;  
4     int var4 = var1 & var2;  
5   }

In the above example, lines 3 and 4 are noncompliant because bitwise operators are applied to Boolean variables. Klocwork produces a CXX.BITOP.BOOL_OPERAND defect at lines 3 and 4, indicating “Bitwise operators should be applied to constants only.”

Fixed code example

1   void func(bool var1, bool var2)
2   {
3     int var3 = 1 | 2;  
4     int var4 = 3 & 4;  
5   }

In the fixed example, Klocwork no longer reports the defect CXX.BITOP.BOOL_OPERAND at lines 3 and 4.