Logical vs. bitwise operators. Both are wrong?

Logical vs. bitwise operators. Both are wrong?


11

When I use the following minimal code in an c++ ConsoleApp in VS2019, 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 VS2019 warning goes away?

The warning is not from the compiler, the output is error-free.
Maybe it comes from Resharper c++ module or from VS 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).

Share
Improve this question

26

  • 4

    It could be because of the type. It could be because the static analyzer thinks none of the expressions make sense. Or it could be a false positive. When and where are you getting these warnings? From the VS IntelliSense? From the compiler? From some third-party analyzer tool? Have you tried to search for the error in the documentation? What does it say?

    – Some programmer dude

    11 hours ago


  • 2

    Have you read learn.microsoft.com/en-us/cpp/ide/… According to the link op1 || op2 should only be used with booleans and op1 | op2 should only be used with integers, not chars

    – jabaa

    11 hours ago


  • 2

    @Rodney Yes, of course, you can do this. But Microsoft doesn't recommend it and its linter gives you a warning.

    – jabaa

    11 hours ago


  • 5

    @woelfchen42 it isn't a false-positive. It is a true positive of a style rule you disagree with.

    – Caleth

    11 hours ago

  • 3

    Please don't post code in comments. It's not readable. Edit your question and add it there.

    – jabaa

    10 hours ago

1 Answer
1

Reset to default


18

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 operatos should only be used with integers, not chars:

unsigned int op1 = 0x1;
unsigned int op2 = 0x3;
unsigned int result2 = op1 | op2;

See https://learn.microsoft.com/en-us/cpp/ide/lnt-logical-bitwise-mismatch?view=msvc-170 for more details

If you don’t want this warning, you can configure the linter in "Options" -> "Text Editor" -> "C/C++" -> "Code Style" -> "Linter": https://learn.microsoft.com/en-us/cpp/ide/cpp-linter-overview?view=msvc-170#configure-the-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.

Share
Improve this answer

10

  • 1

    really ? What about? int *p; float *y; /* ... */; if(p && y) { do_something_();}

    – 0___________

    11 hours ago


  • 1

    @0___________ From the posted link: "Only use logical operators on Boolean values." Disable the linter warning, if you don't want it. It's only a linter rule and a recommendation.

    – jabaa

    11 hours ago


  • 3

    Do I really get downvotes for Microsoft's recommendations or is something wrong with my answer?

    – jabaa

    11 hours ago

  • 1

    @HolyBlackCat The sentence starts with "It tells you that…" after "lnt-logical-bitwise-mismatch is a Visual Studio linter rule." It's not my opinion. It's a fact. I'm not here to discuss whether these rules make sense or not. I want to describe when the squiggles appear and how to disable them.

    – jabaa

    11 hours ago


  • 5

    "Do I really get downvotes for Microsoft's recommendations" My money on the positive answer. Have an upvote to compensate.

    – YSC

    8 hours ago



Leave a Reply

Your email address will not be published. Required fields are marked *