jeudi 18 avril 2019

What does this particular IF statement in C actually do (that also works)?

I came across some sample code which works perfectly for my application, but I am not sure what is actually going on in the IF statement.

unsigned int steps;
uint16_t selected_steps; 

for (int j = 16; j>=0; j--)
{
    if (((selected_steps^(1 << step_number))>>j) & 0x1) {
          some code...
      } else {
          other code...
      }

I know the purpose of the code, it is to check if the bit at selected_steps[j] XOR step_number is 1 or 0. Selected_steps[j] is either 0 or 1. Step number takes values between 0-15.

But I don't understand what is actually done by doing

(1 << step_number)

and then what is done by doing

selected_steps^(1 << step_number)) >> j

I suppose that the above statement outputs a 1 or 0 since it checks against 0x1?

Aucun commentaire:

Enregistrer un commentaire