// 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; uint64_t my_one = 1; //j = 1 << 33; // undefined - constant 1 is an int j = my_one << 33; j = 1lu << 3; j = (uint64_t) 1 << 33; printf("%ld\n",j); uint32_t k; k = 1u << 31; // constant 1 is an int!! This is a signed shift printf("%u\n",k); return 0; }