When I use the following minimal code in an C++ console application in Visual Studio 2019, I get two warnings, which are completely opposite.
int main()
{
unsigned char op1 = 0x1;
unsigned char op2 = 0x3;
unsigned char result1 = op1 | op2;
unsigned char result2 = op1 || op2;
}
The warning at unsigned char result1 = op1 | op2;
is
lnt-logical-bitwise-mismatch Using bitwise '|' when logical '||' was probably intended.
The warning at unsigned char result2 = op1 || op2;
is
lnt-logical-bitwise-mismatch Using logical '||' when bitwise '|' was probably intended.
This is a little bit curious.
My intention was to use the bitwise operator. How could I change the line unsigned char result1 = op1 | op2;
, so that the Visual Studio 2019 warning goes away?
The warning is not from the compiler; the output is error-free. Maybe it comes from the ReSharper C++ module or from Visual Studio code analysis.
(Of course I could ignore that warning, but in the original code there are a lot of them, because there a lot of unsigned char bitwise operations.)
lnt-logical-bitwise-mismatch
is a Visual Studio linter rule. It tells you that logical operators should only be used with booleans:
bool op1 = true;
bool op2 = false;
bool result2 = op1 || op2;
And bitwise operators should only be used with integers, not chars:
unsigned int op1 = 0x1;
unsigned int op2 = 0x3;
unsigned int result2 = op1 | op2;
See here for more details.
If you don't want this warning, you can configure the linter in Options -> Text Editor -> C/C++ -> Code Style -> Linter.
I'm not saying that these rules make sense. I'm just answering why you get the squiggles and how you can avoid it. The usefulness of these rules is a different question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With