Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are Left Shift and Right Shift Operators (>> and <<) in C#?
The left shift (<<) and right shift (>>) operators in C# are bitwise operators that shift the bits of a number left or right by a specified number of positions. These operators are commonly used for efficient multiplication and division by powers of 2, as well as for low-level bit manipulation.
Syntax
Following is the syntax for the left shift operator −
result = operand << numberOfPositions;
Following is the syntax for the right shift operator −
result = operand >> numberOfPositions;
How Left Shift Works
The left shift operator (<<) moves all bits in the left operand to the left by the number of positions specified by the right operand. Empty positions on the right are filled with zeros. Left shifting by n positions is equivalent to multiplying by 2n.
Example
using System;
class Program {
static void Main(string[] args) {
int a = 60; /* 60 = 0011 1100 */
int c = 0;
c = a << 1; /* Left shift by 1: 60 * 2 = 120 */
Console.WriteLine("60 << 1 = {0}", c);
c = a << 2; /* Left shift by 2: 60 * 4 = 240 */
Console.WriteLine("60 << 2 = {0}", c);
c = a << 3; /* Left shift by 3: 60 * 8 = 480 */
Console.WriteLine("60 << 3 = {0}", c);
}
}
The output of the above code is −
60 << 1 = 120 60 << 2 = 240 60 << 3 = 480
How Right Shift Works
The right shift operator (>>) moves all bits in the left operand to the right by the number of positions specified by the right operand. For positive numbers, empty positions on the left are filled with zeros. Right shifting by n positions is equivalent to dividing by 2n (integer division).
Example
using System;
class Program {
static void Main(string[] args) {
int a = 60; /* 60 = 0011 1100 */
int c = 0;
c = a >> 1; /* Right shift by 1: 60 / 2 = 30 */
Console.WriteLine("60 >> 1 = {0}", c);
c = a >> 2; /* Right shift by 2: 60 / 4 = 15 */
Console.WriteLine("60 >> 2 = {0}", c);
c = a >> 3; /* Right shift by 3: 60 / 8 = 7 */
Console.WriteLine("60 >> 3 = {0}", c);
}
}
The output of the above code is −
60 >> 1 = 30 60 >> 2 = 15 60 >> 3 = 7
Comparison of Shift Operators
| Operator | Operation | Equivalent To | Example |
|---|---|---|---|
| << | Left Shift | Multiply by 2n | 8 << 2 = 32 |
| >> | Right Shift | Divide by 2n | 32 >> 2 = 8 |
Common Use Cases
Shift operators are commonly used for −
-
Fast multiplication/division by powers of 2
-
Bit manipulation in graphics and embedded programming
-
Memory optimization when working with flags or packed data
Example
using System;
class Program {
static void Main(string[] args) {
// Fast multiplication and division
int num = 5;
Console.WriteLine("Original number: {0}", num);
Console.WriteLine("Multiply by 4 (shift left 2): {0}", num << 2);
Console.WriteLine("Multiply by 8 (shift left 3): {0}", num << 3);
num = 64;
Console.WriteLine("\nOriginal number: {0}", num);
Console.WriteLine("Divide by 4 (shift right 2): {0}", num >> 2);
Console.WriteLine("Divide by 8 (shift right 3): {0}", num >> 3);
}
}
The output of the above code is −
Original number: 5 Multiply by 4 (shift left 2): 20 Multiply by 8 (shift left 3): 40 Original number: 64 Divide by 4 (shift right 2): 16 Divide by 8 (shift right 3): 8
Conclusion
The left shift (<<) and right shift (>>) operators in C# provide efficient ways to multiply and divide by powers of 2 by shifting bits left or right. Left shift multiplies by 2n, while right shift divides by 2n, making them valuable for performance-critical applications and bit manipulation tasks.
