# Left Shift and Right Shift Operators in C/C++

**<< (left shift) **Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. Or in other words left shifting an integer “x” with an integer “y” (x<<y) is equivalent to multiplying x with 2^y (2 raise to power y).

`/* C++ Program to demonstrate use of left shift ` ` ` `operator */` `#include<stdio.h> ` `int` `main() ` `{ ` ` ` `// a = 5(00000101), b = 9(00001001) ` ` ` `unsigned ` `char` `a = 5, b = 9; ` ` ` ` ` `// The result is 00001010 ` ` ` `printf` `(` `"a<<1 = %d\n"` `, a<<1); ` ` ` ` ` `// The result is 00010010 ` ` ` `printf` `(` `"b<<1 = %d\n"` `, b<<1); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

a<<1 = 10 b<<1 = 18

**>> (right shift) **Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.Similarly right shifting (x>>y) is equivalent to dividing x with 2^y.

`/* C++ Program to demonstrate use of right ` ` ` `shift operator */` `#include<stdio.h> ` `int` `main() ` `{ ` ` ` `// a = 5(00000101), b = 9(00001001) ` ` ` `unsigned ` `char` `a = 5, b = 9; ` ` ` ` ` `// The result is 00000010 ` ` ` `printf` `(` `"a>>1 = %d\n"` `, a>>1); ` ` ` ` ` `// The result is 00000100 ` ` ` `printf` `(` `"b>>1 = %d\n"` `, b>>1); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

a>>1 = 2 b>>1 = 4

Important Points :

- The left shift and right shift operators should not be used for negative numbers. The result of is undefined behaviour if any of the operands is a negative number. For example results of both -1 << 1 and 1 << -1 is undefined.
- If the number is shifted more than the size of integer, the behaviour is undefined. For example, 1 << 33 is undefined if integers are stored using 32 bits. See this for more details.
- The left-shift by 1 and right-shift by 1 are equivalent to multiplication and division by 2 respectively.

As mentioned in point 1, it works only if numbers are positive.`#include<stdio.h>`

`int`

`main()`

`{`

`int`

`x = 19;`

`printf`

`(`

`"x << 1 = %d\n"`

`, x << 1);`

`printf`

`(`

`"x >> 1 = %d\n"`

`, x >> 1);`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**x << 1 = 38 x >> 1 = 9

- The left-shift of 1 by i is equivalent to 2 raised to power i.

As mentioned in point 1, it works only if numbers are positive.`#include<stdio.h>`

`int`

`main()`

`{`

`int`

`i = 3;`

`printf`

`(`

`"pow(2, %d) = %d\n"`

`, i, 1 << i);`

`i = 4;`

`printf`

`(`

`"pow(2, %d) = %d\n"`

`, i, 1 << i);`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**pow(2, 3) = 8 pow(2, 4) = 16

Interesting Facts about Bitwise Operators in C

## Recommended Posts:

- Bitwise right shift operators in Java
- valarray shift() in C++
- Multiplication of two numbers with shift operator
- Operators in C | Set 2 (Relational and Logical Operators)
- Operators in C | Set 1 (Arithmetic Operators)
- Set the Left most unset bit
- ios manipulators left() function in C++
- Can we use function on left side of an expression in C and C++?
- Operators in C / C++
- # and ## Operators in C
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Find position of left most dis-similar bit for two numbers
- C | Operators | Question 6
- C | Operators | Question 17
- C | Operators | Question 18

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.