Can you guys please explain the below program
int main()
{
int max = ~0;
printf("%d\n",max);
return 0;
}
AFAIK ~ will flip the bits. In this case i.e ~0 will set all the bits into 1. So max variable should contain MAX value but I am getting o/p as -1. So can anyone here please tell me why I am getting o/p as -1.
Why did you expect to obtain the “max value”? In 2’s-complement signed representation all-1 bit pattern stands for
-1. It is just the way it is.Maximum value in 2’s-complement signed representation is represented by
01111...1bit pattern (i.e the first bit is0). What you got is1111...1, which is obviously negative since the very first bit – sign bit – is1.If you want an example where complemented zero produces a “max value”, use unsigned representation