- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
C# - Bitwise Operators
C# bitwise operators allow operations at the binary level. These operators are primarily used for performance optimization, low-level programming, encryption, networking, and system programming.
What Are Bitwise Operators in C#?
C# bitwise operators manipulate individual bits of numbers. These operators are commonly used for efficient computations, flag operations, and hardware interactions.
List of C# Bitwise Operators
The Bitwise operators supported by C# are listed in the following table. Assume variable A holds 60 and variable B holds 13, then −
| Operator | Description | Example |
|---|---|---|
| & | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) = 12, which is 0000 1100 |
| | | Binary OR Operator copies a bit if it exists in either operand. | (A | B) = 61, which is 0011 1101 |
| ^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) = 49, which is 0011 0001 |
| ~ | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. | (~A ) = -61, which is 1100 0011 in 2's complement due to a signed binary number. |
| << | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 = 240, which is 1111 0000 |
| >> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 = 15, which is 0000 1111 |
Understanding Bitwise Operations with Examples
Example 1: Bitwise AND (&)
Bitwise AND sets each bit to 1 only if both operands have 1 in that position.
using System;
class Program
{
static void Main()
{
int a = 5, b = 3; // Binary: a = 0101, b = 0011
int result = a & b; // 0101 & 0011 = 0001 (1)
Console.WriteLine("Bitwise AND: " + result);
}
}
When the above code is compiled and executed, it produces the following result −
Bitwise AND: 1
Example 2: Bitwise OR (|)
Bitwise OR sets each bit to 1 if either operand has 1.
using System;
class Program
{
static void Main()
{
int a = 5, b = 3; // Binary: a = 0101, b = 0011
int result = a | b; // 0101 | 0011 = 0111 (7)
Console.WriteLine("Bitwise OR: " + result);
}
}
When the above code is compiled and executed, it produces the following result −
Bitwise OR: 7
Example 3: Bitwise XOR (^)
Bitwise XOR sets each bit to 1 if the corresponding bits are different.
using System;
class Program
{
static void Main()
{
int a = 5, b = 3; // Binary: a = 0101, b = 0011
int result = a ^ b; // 0101 ^ 0011 = 0110 (6)
Console.WriteLine("Bitwise XOR: " + result);
}
}
When the above code is compiled and executed, it produces the following result −
Bitwise XOR: 6
Example 4: Bitwise NOT (~)
Bitwise NOT inverts each bit (turns 1 into 0 and vice versa).
using System;
class Program
{
static void Main()
{
int a = 5; // Binary: 00000101
int result = ~a; // NOT 00000101 = 11111010 (Twos Complement)
Console.WriteLine("Bitwise NOT: " + result);
}
}
When the above code is compiled and executed, it produces the following result −
Bitwise NOT: -6
Example 5: Left Shift (<<)
Left shift (<<) moves bits left, multiplying the number by 2^n.
using System;
class Program
{
static void Main()
{
int a = 5; // Binary: 00000101
int result = a
When the above code is compiled and executed, it produces the following result −
Left Shift: 20
Example 6: Right Shift (>>)
Right shift (>>) moves bits right, dividing the number by 2^n.
using System;
class Program
{
static void Main()
{
int a = 20; // Binary: 00010100
int result = a >> 2; // Right Shift by 2: 00000101 (5)
Console.WriteLine("Right Shift: " + result);
}
}
When the above code is compiled and executed, it produces the following result −
Right Shift: 5
Real-World Applications of Bitwise Operators
Example 7: Checking If a Number Is Even or Odd
You can use bitwise AND to determine if a number is even or odd.
using System;
class Program
{
static void Main()
{
int number = 10;
if ((number & 1) == 0)
Console.WriteLine("Even");
else
Console.WriteLine("Odd");
}
}
When the above code is compiled and executed, it produces the following result −
Even
Example 8: Swapping Two Numbers Without Using a Temporary Variable
You can use bitwise XOR bitwise operator to swap two numbers without extra memory.
using System;
class Program
{
static void Main()
{
int a = 5, b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
Console.WriteLine("After Swap: a = " + a + ", b = " + b);
}
}
When the above code is compiled and executed, it produces the following result −
After Swap: a = 10, b = 5
Common Mistakes & How to Avoid Them
Mistake
Issue
Solution
a << -1
Negative shift count
Use a << 0 or a >> 0
a = a & 0;
Always sets a to 0
Ensure a & mask where mask is properly set
b = ~b;
Unexpected negative value
Understand twos complement representation
Best Practices for Using Bitwise Operators in C#
- Use bitwise AND (&) for flag operations and bit masking.
- Use XOR (^) for swapping values efficiently.
- Use shift operators (<< and >>) for fast multiplication and division by powers of 2.
- Understand how negative numbers are represented in twos complement format.