Using gcc compiler, why is this uint32_t
-2147483648 when I set the MSB? It's unsigned - shouldn't it be positive?
#include <stdio.h>
#include <stdint.h>
#define BIT_SET(a,b) ((a) |= (1<<(b)))
int main()
{
uint32_t var = 0;
BIT_SET(var, 31);
printf("%d\n", var); //prints -2147483648
return 0;
}
Type of var
is uint32_t
. But you are printing it using %d
which is undefined behaviour. Use PRIu32
from <inttypes.h>
to print uint32_t
:
printf("%"PRIu32"\n",var);