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

Discussion

In the statement expression1 >> expression2. if expression1 is a signed integer with its leftmost bit set to 1 then on right shifting it the result of the statement will vary from computer to computer

  • A.True
  • B.False

Answer: A

Correct answer: True

Right-shifting a signed value with the sign bit set is implementation-defined in C: the standard permits either an arithmetic shift, propagating the sign bit so the value stays negative, or a logical shift, bringing in zeros so it becomes a large positive number.

In practice every common compiler chooses the arithmetic shift, but portable code cannot rely on it because the standard does not require it. The safe course is to cast to an unsigned type when the bit pattern is wanted, and to divide when the arithmetic result is wanted.

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

Post your comment

Please log in or register to post a comment.