// Andrew Taylor - andrewt@unsw.edu.au // 08/06/2020 // Examples of illegal bit-shift operations #include #include int main(void) { // int16_t is a signed type (-32768..32767) // below operations are undefined for a signed type int16_t i; i = -1; i = i >> 1; // undefined - shift of a negative value printf("%d\n", i); // i = -1; // i = i << 1; // undefined - shift of a negative value // printf("%d\n", i); i = 32767; i = i << 1; // undefined - left shift produces a negative value printf("%d\n", i); uint64_t j; //j = 1 << 33; // undefined - constant 1 is an int uint64_t one = 1; j = one << 33; j = 1ul << 33; j = ((uint64_t)1) << 33; // ok printf("%ld\n",j); uint32_t k; //k = 1 << 31; // constant 1 is an int!! This is a signed shift k = 1u << 31; //This is ok. printf("%u\n",k); return 0; }