I looked in this question to understand why there is no arithmetic left shift operator in most languages including Java . But then how do we deal with negative integers because left shifting would throw it away . Take for example -2^5 where the result should be negative .
Share
No, the sign bit will never be thrown out by left shift unless you commit an overflow. Reason? In two’s complement encoding the negative number has all ones on te left side, where a positive number would have all zeros. It’s like the sign bit is replicated to the right. BTW this is exactly why the term “sign extension” makes sense, which is how the default right-shift works in Java.
prints