I want to get the values of INT_MIN
and INT_MAX
. I\'ve tried ~0
and ~0 >> 1
since the leftmost bit is a sign bit bu
0 is of type int
. So are ~0
and ~0 >> 1
because of int type promotion
~0 has all 1s in its bit pattern and it's -1 in 2's complement, which is the default representation of most modern implementations.
Right shift in C is implementation defined. But most implementations define >>
as arithmetic shift when the type is signed and logical shift when the type is unsigned
Since ~0
is int
, which is a signed type, ~0 >> 1
will be an arithmetic shift right. Hence the value is sign extended, cause the value to be all 1s
You need to do unsigned(~0) >> 1
or ~0U
There are no ways to get INT_MIN
and INT_MAX
portably because in C there are 3 different signed type implementations beside trap representations and padding bits. That's why standard libraries always define INT_MIN
and INT_MAX
directly with the values