0
$\begingroup$

According to this Wikipedia article, when arithmetic left shift operation is applied to a signed number, the number is multiplied by 2. But there are certain situations where a negative number becomes a positive number when an arithmetic left shift is applied.

Eg.: Take a 2's complement signed integer -5 and 5 bits are used to represent it.

11011 ==> -5
10110 ==> -10 (-5x2)
01100 ==> +12 (?)

So after two arithmetic left shifts -5 became 24. I expected -20. Why is this the case?

$\endgroup$
  • $\begingroup$ Please explain how you would store -20 in a 5 bit signed integer. $\endgroup$ – gnasher729 Aug 22 '19 at 17:33
2
$\begingroup$

$01100 \Rightarrow +12$, not $+24$.

And it is $+12$ because $32-20=12$. With $5$ bits and $2$s complement you can only represent numbers in the range $-16$ to $+15$. You have overflowed this range and, in effect, "run around" to the other end and into the positive number range.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.