int x = 5; x << 32; is compiled for a platform whose int is 32 bits wide. What is the status of that shift?
Correct answer
A. Undefined behaviour, because the shift count is not less than the operand's width
16 views
A. Undefined behaviour, because the shift count is not less than the operand's width
16 views
Shift operands must satisfy 0 <= count < width, so shifting a 32-bit int by 32 is undefined behaviour, not a guaranteed zero. Compilers exploit that freedom: x86 masks the count to five bits, so the same source quietly computes x << 0 and returns 5. The count is computed in int, so a negative count is equally undefined.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.