Bitwise Operators (Part 2)(Left Shift(<<) Operator)

This is part 2 of the bitwise operator before proceeding with this article please visited part 1 of the bitwise operator.

Let’s first look at what we are cover in this article

Here we are introducing left shift operator and discussing some important points related to the left shift operators.

Let’s introducing left shift operator

1 The left-shift operator takes two operands.

2 The left-shift operator is a binary operator.

Here is the syntax of the left-shift operator, 

Left operand << Right operand

here is how the left-shift operator looks like. In the left-shift operator, the left Operand is that whose get left-shifted. right Operand is the one that decides the number of places to shift the bits.

To understand the left-shift operator we will first cover some important points and meanwhile, we will also understand how the left-shift operator works.

Let’s talk about some important points about the left shift operator.

1.when bits are shifted left then the trailing positions are filled with zeros.

Let’s consider an example to better understand this concept,

/**
C program to demonstrate use of left shift Operator
*/
#include<stdio.h>
int main(){
    char a = 5;
    // binary representation of 5(0000 0101)

    printf("a<<1 = %d\n",a<<1);
    // Result is 0000 1010 which equal to 10 in Decimal

    return 0;
}
In this example, I choose a variable and assigned value 5 to variable a. What I am going to do here, I am going to shift this whole number by one position towards left using this left-shift operator available over here. Here the data type is the character the binary representation of 5 is looks like this (0000 0101).

Now, What would be the output of the particular program, we first need to understand how left shift operator works.

This is the left operand of the left-shift operator and this is the right operand of the left shift operator. The value of a variable is 5 and the binary representation of this value equals 0000 0101. Now, what we will do? we know this thing that the right operand is the one which decides to a number of places to get shifted towards left. here the value is one so that it will shift the bits of this number left to 1 position okay.

a = 5

a << 1

5 = 0000 0101

What would be the result, after the left shift result looks like his.

0000 101_

So this bit shift towards this position.  this bit shift towards this position and so on. Finally, this bit shift towards this position. The last position is empty or vacant, what we can fill in this position. As we know trailing positions are filed with zero. So we will put zero here and finally this would be the output.

0000 1010

As you convert this binary into a decimal you will get 10 as a result. So, the output of the following program is 10.

Now let’s try to understand 2nd most important point of the left shift operator.

2. Left shifting is equivalent to multiplication by 2ʳᶦᵍʰᵗᴼᵖᵉʳᵃⁿᵈ

Let’s understand this concept using the example.

Here is a variable that has a value of 5. And we want to shift left towards one position. We know the output is 10. But how simply 5 is multiplied by 5 * 2. 5 into 2 which equals 10 and that is our output.

a = 5

a << 1     OUTPUT:- 5 [ 5 * 2¹]

Let’s consider one more example

Here is a variable that has a value of 3. And we want to shift left towards four position. We know the output is 48. But how simply 3 is multiplied by 3 * 16. 3 into 16 which equals 48 and that is our output.

a = 3

a << 4     OUTPUT:- 48 [ 3 * 2]


Comment below if you have any queries related to the above tutorial for The left shift (<<) Operators in C.

Post a Comment

0 Comments