cgccbit-manipulationbitwise-operatorssanitizer

1 << 31 cannot be represented by type 'int'?


Why does -fsanitize=undefined throw

runtime error: left shift of 1 by 31 places cannot be represented in type 'int'

on this code

uint32_t z;
z = 1 << 31;

?


Solution

  • Make the 1 unsigned:

    uint32_t z;
    z = UINT32_C(1) << 31;