2
$\begingroup$

I have recently faced a problem .The problem is here.We know that if we represent a decimal number in binary and move left all the bits by one. The left most bit is lost! and at the rightmost, a zero is added.

The above bit operation actually produce a number that is result of multiplication of the given number and 2.

For example, $0001001101110010 ⇒ a = 4978(16 bit)$

---------------- << 1 (SHIFT LEFT the bits by one bit)

$0010011011100100 ⇒ 9956$

My question is that why it happens? Can anyone explain what the reason behind it?

$\endgroup$
  • 2
    $\begingroup$ A number $...cba$ in binary is $... c \cdot 2^2 + b \cdot 2^1 + a \cdot 2^0$. Do you see what happen if you multiply by 2 now? $\endgroup$ – Javier Jan 13 '16 at 13:16
  • $\begingroup$ Hint: in our ordinary base, appending a $0$ multiplies by $10$. $\endgroup$ – Yves Daoust Jan 13 '16 at 13:17
9
$\begingroup$

There is a direct analogous when you work with base $10$.

Take the number $3$ in base $10$. Shift it left: you get $30$, which is $3 \cdot 10$ (and the factor $10$ appears because you are working with base $10$).

The same applies to base $2$. Shifting left is the same as multiplying by $2$.

This comes from the use of positional notation to denote numbers (https://en.wikipedia.org/wiki/Positional_notation).

In base $b$ ($b>1$) the second digit from the right counts $b$ times more than the first digit from the right, the third from the right counts $b$ times more than the second from the right (or $b^2$ times more than the first from the right), and so on.

When you write a number like this

$$ a_n a_{n-1} \dots a_2 a_1 a_0 $$

(in base $b$), what you actually mean is the following

$$ a_n \cdot b^n + a_{n-1} \cdot b^{n-1} + \dots + a_2 \cdot b^2 + a_1 \cdot b^1 + a_0 \cdot b^0.$$

With this in mind one can show that the two operations of shifting left and multiplying by $b$ are actually the same:

$$ a_n a_{n-1} \dots a_2 a_1 a_0 0 = \\ = a_n \cdot b^{n+1} + a_{n-1} \cdot b^{n} + \dots + a_2 \cdot b^3 + a_1 \cdot b^2 + a_0 \cdot b^1 + 0 \cdot b^0 =\\= b \cdot \left(a_n \cdot b^n + a_{n-1} \cdot b^{n-1} + \dots + a_2 \cdot b^2 + a_1 \cdot b^1 + a_0 \cdot b^0 \right) =\\= b \cdot (a_n a_{n-1} \dots a_2 a_1 a_0). $$

$\endgroup$
1
$\begingroup$

Take a sample binary number,

0110 , its value in decimal is (from rightmost to leftmost)
0 * 2^0 +
1 * 2^1 +
1 * 2^2 +
0 * 2^3 = 6.

Now shift all digits 1 bit to the left.
1100

0 * 2^0 +
0 * 2^1 +
1 * 2^2 +
1 * 2^3 = 12.
What you are essentially doing is multiplying all the powers of two by another 2, when you shift the digits to the left. Hope this answers your question. When you shift all digits to the right then through the same logic you are dividing the number by two.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.