What is arithmetic left shift 01001001?

I would think that it is 00010010 that is, it is trying to maintain a sign bit like

On the other hand, a logical left shift of 1 pos would be 10010010

Is it correct?

+7
source share
4 answers

For the left shift, the arithmetic and logical shifts are the same.

The difference is only for a shift to the right, where an arithmetic shift to the right shifts the old MSB to the new MSB after the shift, thereby preserving the negative number from converting to positive when shifting.

Wikipedia provides a more detailed explanation.

+7
source

In Java << there is a logical left shift. 0 is always added as LSB.

(Note that Java will advance the specified value [byte], so care should be taken to mask back to octect! Otherwise, you will save the shifted bit (s), which could include "1".)

However, the Wikipedia article on Arithmetic Shift indicates that arithmetic left shift can lead to an overflow error:

... Please note that arithmetic left shift can cause overflow; this is the only way that differs from a logical left shift.

(This is not the case in Java, but just need to be kept in mind.)

Happy coding.

+4
source

Yes, that's right.

The arithmetic left shift of x by n places is x * (2^n) . So in your example, this is the ar-left shift of 01001001 by 1 , equal to 10010010 (73 * 2ยน = 146).

+1
source

You are right when you left the shift for 1 bit. It is equal to 10010010.

when you shift 4 bits to the left as follows, you get the following response.

 01001001 << 4 = 10010000 

when you shift 4 bits to the right as follows, you get the following response.

 01001001 >> 4 = 00000100 

Bits left empty as a result of the shift are filled with zeros.

0
source

Source: https://habr.com/ru/post/905658/


All Articles