1. Home
  2. Computer Science MCQs
  3. C Programming
  4. int x = 5; x

Added 30 Nov -0001

int x = 5; x << 32; is compiled for a platform whose int is 32 bits wide. What is the status of that shift?

  1. Undefined behaviour, because the shift count is not less than the operand's width Correct answer
  2. Defined to yield zero, because every set bit is shifted out of the 32-bit range entirely
  3. Implementation-defined, so the compiler documents whether it wraps the count
  4. A constraint violation, so a strictly conforming compiler must reject the code

Correct answer

A. Undefined behaviour, because the shift count is not less than the operand's width

16 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs