Bitwise left shift operator
In the previous post, you understood about the right shift operator. Now let’s understand the left shift operator.
Bitwise left shift operator(<<)
‘<<’ is a symbol you use for the left shift operator. This is similar to the right shift operator, the only thing is the operand.
- This operator takes 2 operands.
- Bits of the first operand will be left shifted by the amount decided by the second operand.
- Syntax: operand1 << operand2
Example
Let’s take the number 111.
01101111 is the binary format of the number 111.
Now let’s understand what happens when you left shift by 1( Figure 1).
Here, first let’s start with an MSB(the most significant bit). MSB is 0. 0 is left shifted by 1. 0 is lost. Then, 1 is moved out to MSB position. So, bit position 6 will be left shifted to bit position 7, bit position 5 will be shifted to its left, like that. And LSB becomes an empty space, where 0 will be shifted in.
That’s why 11011110 is a result after you left shift 01101111 by 1. You should observe that 0 occupied the LSB.
10111100 is a result after you left shift by 2, 01111000 is a result after you left shift by 3, and after you left shift by 4, you get 11110000.
In Figure 1, you can see that the first four least significant bits(LSB) are 1111, which is a lower nibble side. 0110 is actually to the higher nibble side. After the left shift by 4, 0110 (the higher bits) are lost and four zeros actually occupied the lower number.
And also you should note that the original value was 111, now it is increased to 240.
In the previous case, that is in the case of right shift operation the value was decreased. The result was 6 in that case. In the left shift operator, the final value is increased.
Difference between the left shift and the right shift bitwise operator:
When you do left shift on a number, the weight of the number will increase.
For example, here 4 is left shifted by 0 (4<<0). So, no effect.
4 is left shifted by 1, the final value will be 8. So, the value doubled. If you left shift by 1, the value again doubles here, it is 16, like that.
So, when you do the left shift the value increases. That’s because the weightages of the bit positions will increase if you left shift a number.
Now contrary to that when you right shift a number the value decreases.
You can see that in Figure 2, 128 when it is right shifted by 2 (128 >>2), it becomes 32. So, it’s like 128/2 to the power 2.
So, whereas 4 when it is left shifted by 5 (4<<5), it becomes 128. It’s like 4×2 to the power 5.
That’s because, when you do the left shift operation the weightages of the bit positions increase, and when you do the right shift operation the weightages of the bit positions decrease.
A value will be multiplied by 2 for each left shift.
A value will be divided by 2 for each right shift.
So, all these things doesn’t matter actually when we do programming. That’s why we have to understand the applicability of bitwise shift operations in embedded programming.
All those things are theories actually, but we need to understand what are the real applicability of bitwise shift operation in embedded programming code. And we’ll understand that in the next article.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1