//filename:mt.c
//filename is useful to understand the gcc command
#include <stdio.h>
int isTmax(int x);
int main()
{
printf("wtf %d\n", isTmax(0x7fffffff));
return 1;
}
int isTmax(int x)
{
int y = ((x + x + 2) ^ 1);
int z = (!(~(x + x + 2) + 1) ^ 0);
printf("y = %d\n", y);
printf("z = %d\n", z);
return y & z;
}
The code is weird because it was a csapp handout solution(obviously a wrong one).
(x+x+2)
equals 0 when x equals 0x7fffffff. So (~(x+x+2)+1)
equals 0 because of overflow. So (!(~(x+x+2)+1)^0)
equal 1. Watch during debugging verified that.
In my opinion, under normal conditions, z should be 1 after the assignment.
Environment:{Systerm:windows 10; Virtual systemubuntu 20.04 LTS; Virtual machine software: VirtualBox 6.1; GCC:gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0}
This pic has more detail. VScode was connecting the virtual system described above.
The exact same code behaved differently in another environment. Another environment:{Systerm:windows 10; Virtual system:ubuntu 20.04 LTS; Virtual machine software:VirtualBox 6.1; GCC:gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609}