I would like to test bit size of unsigned long using macro. One would think that if you specify 0UL it will evaluate as unsigned long, but according this short example it is not true. I compiled it using MinGW on 32bit architecture, so ~0UL should get me 0xFFFFFFFF:
#if (((~0UL) >> 31) >> 1) // expected 0, double shift to get around mod 32 limit
#define UL_BIT_SIZE 64
#else
#define UL_BIT_SIZE 32
#endif
printf("%d, %X\n", UL_BIT_SIZE, ((~0UL) >> 31) >> 1);
Output: 64, 0
Funny thing is that if I change UL to L or simply skip the postfix it’s working as expected. Can anyone please explain what’s the problem here, what’s the cause and how can it be solved?
C preprocessor does not know types or C keywords.
Preprocessor arithmetic is done with the largest integer type from
stdint.hwhich isintmax_toruintmax_tdepending on the sign of the operands.