Why does shifting right on a two’s complement binary number divide it by 2

binarybinary operations

I understand how logical right shift works. Given an unsigned binary number $n = a_{n-1}a_{n-2}…a_0$, digit $a_k$ contributes $a_k\times 2^k$ to the value of the $n$. after applying a logical right shift, this digit will contribute $a^k\times 2^{k-1}$ and the result will be $\lfloor\dfrac{n}{2}\rfloor$ (rounding towards zero because we lose $2^0$).

I also understand how two's complement works, but have yet to understand how arithmetic right shift divides a negative number by $2$ and rounds down towards negative infinity (as mentioned in Wikipedia).

Best Answer

Let $\,W\,$ be the word length of a bit field, $\,N=2^W\,$ be the number of different words.The two's complement choice to represent a negative integer $\,x\,$ is $\,N+x \,$ (essentially modulo $\,N\,$). A logical right shift gives the result $\,\lfloor \frac{N+x}2\rfloor.\,$ To convert this to an arithmetic right shift we have to add a sign bit $\,\frac{N}2.\,$ Thus the result becomes $$ \frac{N}2 + \left\lfloor\frac{N+x}2\right\rfloor = \left(\frac{N}2+\frac{N}2\right) + \left\lfloor\frac{x}2\right\rfloor = N + \left\lfloor\frac{x}2\right\rfloor. $$ However, this is the two's complment representation of $\,\left\lfloor\frac{x}2\right\rfloor.\,$