In my example im creating an integer variable called testVar and i make it equal to a1 in hex. when printing out the variable it shows that it should indeed be equal to a1 in hex. Now when checking if testVar is equal to a1 in an if statement, the if statement only executes when there is no mask applied to the variable.
My question “is why these if statements don’t execute when the variable has a mask applied to it?”.
#include <stdio.h>
int testVar = 0xa1; //testVar should equal 0x000000a1 now
void main(){
printf("%x n",testVar); //should prove that testVar is indeed equal to a1 in hex
printf("%x n",testVar & 0x000000ff); //both printf functions are outputting a1
if (testVar == 0x000000a1){ //this if statement works!
printf("success1");
}
if (testVar & 0x000000ff == 0x000000a1){ //this doesn't execute
printf("success2");
}
if (testVar & 0x000000ff == 0xa1){ //this doesn't execute
printf("success3");
}
}
New contributor
bangingmyheadontable is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.