Home > Enterprise >  bitwise OR and AND in c
bitwise OR and AND in c

Time:10-13

I have

#define MS 0x0100|0x011

I need to check 0x011 present in MS. I use MS & 0x011 which is giving value present. But id MS & 0x10 also giving true value, I need it as false

CodePudding user response:

  • (value & mask) != 0 : is at least one of the set mask bits also set in value?
    (This is what value & mask is in a boolean context.)
  • (value & mask) == mask : are all the set mask bits also set in value?

That is, the first is like "or" and the second like "and".

CodePudding user response:

(Edited) You need

((MS) & 0x11) == 0x11

Thanks to lorro for his comment

CodePudding user response:

The 0x0100 | 0x011 will result in 0x111 which will result in all & tests for 0x1, 0x10, 0x100, 0x11, 0x110 and 0x111 to evaluate true.

You need to test your values directly, and will not be able to use your #define.

  • Related