Bitwise shifts can be used to multiply or divide nmbers by powers of 2 simply by moving all the bits of a binary number a certain number of places to the left or right.
As you can see in the example below when we bit shift left we double the number eah time. So if we bitshift left by 2 places we multiply the number by 4. If we shift 3 places we multiply by 8 and so on.
| Columns | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | Decimal |
|---|---|---|---|---|---|---|---|---|---|
| Number | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 7 |
| Leftshift1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 14 |
| Leftshift2 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 28 |
| Leftshift3 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 56 |
When we right shift we divide our number as can be seen in the example below.
| Columns | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | Decimal |
|---|---|---|---|---|---|---|---|---|---|
| Number | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 56 |
| Rightshift1 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 28 |
| Rightshift2 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 14 |
| Rightshift3 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 7 |
Use the area below to generate a random bit shift problem.
For our example let's do a left shift of 2 places on the binary number 00010101
| Columns | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | Decimal |
|---|---|---|---|---|---|---|---|---|---|
| Number | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 21 |
| Leftshift2 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 84 |
| Columns | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | Decimal |
|---|---|---|---|---|---|---|---|---|---|
| Number | |||||||||
| Shifted |
Generate a bit shift question and check your answer below