Menu

Is there a check for misusing bitwise operators?

2024-08-01
2024-08-02
  • Steve Albright

    Steve Albright - 2024-08-01

    I've seen this mistake made at multiple companies and it seems like an easy check to add if it doesn't already exist (passed without issue for me).

    bool bitwiseIncorrectUsageExample()
    {
    bool b1 = std::rand() % 2 == 1;
    bool b2 = std::rand() % 3 == 1;

    return b1 & b2;
    }

     
  • CHR

    CHR - 2024-08-01

    with --enable=style:
    test.cpp:6:11: style: inconclusive: Boolean expression 'b1' is used in bitwise operation. Did you mean '&&'? [bitwiseOnBoolean] return b1 & b2; ^

     
  • Steve Albright

    Steve Albright - 2024-08-01

    Well, I swear I've seen the warning before. I am using 2.14 in Ubuntu 22.04 and have pulled and built it myself and I'm already using --enable=all.

    I went ahead and made a new file called test.cpp and put that code in it and did

    cppcheck --enable=style test.cpp

    and I don't get a warning.

     
  • CHR

    CHR - 2024-08-02

    --inconclusive needs to be passed as well. I always use that flag, so it's easy to forget about it.

     
  • Steve Albright

    Steve Albright - 2024-08-02

    Yes, now I see it, thanks. Maybe this one shouldn't be inconclusive?

     

    Last edit: Steve Albright 2024-08-02

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.