What's the difference between right shift(>>) and Unsigned right shift(>>>)
In fact, the right shift(>>) is the Arithmetic shift and the Unsigned right shift is the Logical shift .
The difference is a logical shift does not preserve a number's sign bit .
In order to do the bitwise operation, the decimal representation must be transformed into a two's complement internally first.
The process of 5 > > > 2 = 1 5 >>> 2 = 1 5 >>> 2 = 1
The positive number is the same in both sign-magnitude and two's complement representation , so it's pretty easy:
5 10 → 00000000000000000000000000000101 5_{10} \to 00000000000000000000000000000101 5 10 → 00000000000000000000000000000101 in both sign-magnitude and two's complement representation.
Logical shift: 00000000000000000000000000000101 → 00 → 00000000000000000000000000001 01 00000000000000000000000000000101 \to \underrightarrow{00}00000000000000000000000000001\cancel{01} 00000000000000000000000000000101 → 00 00000000000000000000000000001 01
The result is 1 10 1_{10} 1 10
The process of − 5 > > > 2 = 107374182 2 10 -5 >>> 2 = 1073741822_{10} − 5 >>> 2 = 107374182 2 10
5 10 → 00000000000000000000000000000101 5_{10} \to 00000000000000000000000000000101 5 10 → 00000000000000000000000000000101 in sign-magnitude representation
− 5 10 → 1 ‾ 0000000000000000000000000000101 -5_{10} \to \underline{1}0000000000000000000000000000101 − 5 10 → 1 0000000000000000000000000000101 in sign-magnitude representation
Convert the sign–magnitude representation to two's complement one via Working from LSB towards MSB which is more clear than from the one's complement : 10000000000000000000000000000101 → 1 ‾ 111111111111111111111111111101 1 ‾ 10000000000000000000000000000101 \to \underline{1}111111111111111111111111111101\underline{1} 10000000000000000000000000000101 → 1 111111111111111111111111111101 1 .
Logical shift: 11111111111111111111111111111011 → 00 → 111111111111111111111111111110 11 11111111111111111111111111111011 \to \underrightarrow{00}111111111111111111111111111110\cancel{11} 11111111111111111111111111111011 → 00 111111111111111111111111111110 11
Convert from two's complement representation to decimal via the following formula: w = − a N − 1 2 N − 1 + ∑ i = 0 N − 2 a i 2 i w = -a_{N-1}2^{N-1} + \sum_{i=0}^{N-2}a_i2^i w = − a N − 1 2 N − 1 + ∑ i = 0 N − 2 a i 2 i
The result is 107374182 2 10 1073741822_{10} 107374182 2 10
The process of − 9 > > > 2 = 107374182 1 10 -9 >>> 2 = 1073741821_{10} − 9 >>> 2 = 107374182 1 10
9 10 → 00000000000000000000000000001001 9_{10} \to 00000000000000000000000000001001 9 10 → 00000000000000000000000000001001 in sign-magnitude
− 9 10 → 1 ‾ 0000000000000000000000000001001 -9_{10} \to \underline{1}0000000000000000000000000001001 − 9 10 → 1 0000000000000000000000000001001 in sign-magnitude
10000000000000000000000000001001 → 1 ‾ 111111111111111111111111111011 1 ‾ 10000000000000000000000000001001 \to \underline{1}111111111111111111111111111011\underline{1} 10000000000000000000000000001001 → 1 111111111111111111111111111011 1 in two's complement
11111111111111111111111111110111 → 00 → 111111111111111111111111111101 11 11111111111111111111111111110111 \to \underrightarrow{00}111111111111111111111111111101\cancel{11} 11111111111111111111111111110111 → 00 111111111111111111111111111101 11
The result is 107374182 1 10 1073741821_{10} 107374182 1 10