Machine-translated by Google. Questions are authored in English.
Top

Discussion

Left shifting an unsigned int or char by 1 is always equivalent to multiplying it by 2.

  • A.True
  • B.False

Answer: A

Correct answer: True

For unsigned types the arithmetic is defined to wrap modulo \( 2^n \), so a left shift by one is exactly a multiplication by two with any overflow discarded rather than undefined.

That is the whole difference from the signed case, where shifting a value whose significant bits reach the sign position is undefined behaviour. The char in the question is promoted to int before the shift, so the operation happens in a wider type; the guarantee is what makes shift-based multiplication safe on unsigned values and unsafe on signed ones.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.