Why x_temp is not updating the value, where as the commented line x &= ~(1 << i); is working perfectly.
Where it is going wrong?
int x = 0x4567;
int x_temp = 0x0;// = 0xF0FF;
int y = 0x0200;
int i;
for(i = 8; i < 12; i++)
{//clean clear
x_temp = x & ~(1 << i);
//x &= ~(1 << i); //This line works perfectly.
}
printf("x_temp = %x....\n", x_temp);//Still it retains the value 0x4567.
printf("x = %x....\n", x);
y = x|y; //y = x_temp|y;
printf("y = %x\n", y);
In the last iteration of your loop,
iis 11, but the 11th bit ofxis already 0, so the result is 0x4567. I don’t know why you expect something else. In the case ofx &= ~(1 << i), you clear a bit in the previous value ofx, whereas withx_tempyou keep assigning a fresh value tox_temp… one case is cumulative, the other is not.Consider a trace of the two loops:
Maybe this is clearer:
Suppose x = 5; then a loop that sets x += 1 will yield values of 6,7,8,9,10, …
but a loop that sets x_temp = x + 1 will yield values of 6,6,6,6,6,…