Search code examples
c++operatorsbit-shiftmasking

Is my masking correct


What does it mean by

if ((readParameter - > type(0) & 0xff) == 0xff) {}

I know when we dO '&'with oxff then it returns the LSB. But what does it mean by evaluating it with again == 0xff ?

I feel it something like this(for instance) :

 if ((00000000 00000000 00000000 11011001 & 00000000 00000000 00000000 11111111) == 00000000 00000000 00000000 11111111) 
   {
     //IF THEY ARE EQUAL IT ENTERS IN THE LOOP ? IN THIS CASE THEY ARE  NOT EQUAL
   }

Please correct me if i am wrong ?


Solution

  • But what does it mean by evaluating it with again == 0xff ?

    this if checks if least significant byte is equal to 0xff. The rest of what readParameter->type(0) returns might contain other bits set. If they were not removed with & 0xff then equality to 0xff might never be true.

    I know when we dO '&'with something then it returns the LSB.

    this is not true, when you use binary bitwise AND then the result depends on the arguments used in the operation. If you & with 0xff then you will get least significant byte, but if you do (ui32value & 0xff000000) >> 24 then you will read the most significant byte.