Bitwise Right Shift Operator in C

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Bitwise Right Shift Operator in C

1.
2.

It is denoted by >>
Bit Pattern of the data can be shifted by specified number of
Positions to Right
3. When Data is Shifted Right , leading zeros are filled with zero.
4. Right shift Operator is Binary Operator [Bi two]
5. Binary means , Operator that require two arguments

Quick Overview of Right Shift Operator


Original Number A

0000 0000 0011 1100

Right Shift by 2

0000 0000 0000 1111

Leading 2 Blanks

Replaced by 0 ,Shown in RED

Direction of Movement of Data

Right ========>>>>>>

Syntax :
[variable]>>[number of places]

Live Example : Bitwise Operator [Right Shift Operator]


#include<stdio.h>
int main()
{
int a = 60;
printf("\nNumber is Shifted By 1 Bit : %d",a >> 1);
printf("\nNumber is Shifted By 2 Bits : %d",a >> 2);
printf("\nNumber is Shifted By 3 Bits : %d",a >> 3);
return(0);
}

Output :
Number is Shifted By 1 Bit : 30
Number is Shifted By 2 Bits : 15
Number is Shifted By 3 Bits : 7

Bitwise Left Shift Operator in C


1.
2.
3.
4.
5.

It is denoted by <<
Bit Pattern of the data can be shifted by specified number of Positions to Left
When Data is Shifted Left , trailing zeros are filled with zero.
Left shift Operator is Binary Operator [Bi two]
Binary means , Operator that require two arguments

Quick Overview of Left Shift Operator


Original Number A

0000 0000 0011 1100

Left Shift

0000 0000 1111 0000

Trailing Zeros

Replaced by 0
(Shown in RED)

Direction of Movement of Data

<<<<<=======Left

Syntax : Bitwise Left Shift Operator


[variable]<<[number of places]

Live Example : Bitwise Operator [Left Shift Operator]


#include<stdio.h>
int main()
{
int a = 60;
printf("\nNumber is Shifted By 1 Bit : %d",a << 1);
printf("\nNumber is Shifted By 2 Bits : %d",a << 2);
printf("\nNumber is Shifted By 3 Bits : %d",a << 3);
return(0);
}

Output :
Number is Shifted By 1 Bit : 120
Number is Shifted By 2 Bits : 240
Number is Shifted By 3 Bits : 480

Explanation of Left Shift Binary Operator :


We know the binary representation of the 60 is as below

0000 0000 0011 1100

Now after shifting all the bits to left towards MSB we will get following bit pattern
0000 0000 0011 1100
<< 1
------------------0000 0000 0111 1000

= 60
= 120

Observations and Conclusion :


Shfting Binary 1 to Left By X Bits

Decimal Value

Converted Value

0 Bit

2^0

1 Bit

2^1

2 Bits

2^2

3 Bits

2^3

4 Bits

2^4

16

5 Bits

2^5

32

6 Bits

2^6

64

7 Bits

2^7

128

8 Bits

2^8

256

You might also like