Let’s generalize:
Suppose that you have n+1
digits, noted D0
to Dn
starting with least significant one (i.e. starting on the right in our numeral conventions). Suppose that R
is the radix used, so that every Di
has a value between 0
and R-1
.
The mathematical value V
expressed by the sequence of digits is the sum (for i=0 to n) of Di*R^i
(where ^
expresses the power operation).
Examples, with V expressed in decimal:
R=10. D=265. V=5+6*10+2*100=265
R=16. D=A2. V=2+A*16=2+10*16=162
R=2. D=101. V=1+0*2+1*4=5
You can then easily demonstrate that shifting any such numbers in radix R
to the left by introducing an additional 0
digit on the right means multiplying its value by R
.
R=10. D=265. V=265. 26550 is V*10
R=16. D=A2. V=162. A20 is V*16
R=2. D=101. V=5. 1010 is V*2
So in a binary system, with bits being in radix 2 the shift left is multiplying by 2.
But no hardware works with infinite number of bits
When you use unsigned numbers, and if you have a maximum of n+1
digits, then for any number with Dn
not null (so greater or equal than R^n
), shifting to the left once more means to lose the highest digit of value Dn*R^n
Example:
R=10 n+1=3 D=801 => 010 so V*10-8*100
R=2. n+1=8. D=10000101 => 00001010 so V*2-1*128
Now things are more complex for signed numbers. Here, in mathematical notation, the sign is always an extra information that is never confused with digits.
But in a computer, the sign is represented by a bit (generally the most significant one) and anyway the other bits may represent a two’s complement for negative numbers.
I let you also as a simple exercise to calculate the rotation effect.
I let you as a more advanced exercise for a given binary encoding of a signed number to calculate the effect of shifting when it affects the sign bit.