The & operator cannot be applied to bytes, int, boolean

In my previous question on how to compare, if the combined bits contain a specific bit, I am triggering this error.

    int flag1 = 1 << 0;
    int flag4 = 1 << 5;

    int combined = flag1 | flag4;

    if (combined & flag1 == flag1) // <-- Operator & cannot be applied to int, boolean

If I make flags in bytes, then the error replaces intwith byte.

+4
source share
1 answer

The compiler sees the binary operator &in your expression if, treats it as a logical AND (since it expects an expression that returns boolean), and checks the types of arguments.

int - combined - boolean - flag1 == flag1. boolean ( & int a boolean), .

, :

if ((combined & flag1 ) == flag1)
+9

All Articles