Introduction
Bitwise operators are used to perform operations on individual bits of integer types. These operators treat their operands as a sequence of bits (binary representation) rather than as decimal, hexadecimal, or octal numbers. Bitwise operations are fundamental in low-level programming, such as device drivers, cryptography, and network protocol implementation.
List of Bitwise Operators
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise Complement (~)
- Left Shift (<<)
- Right Shift (>>)
1. Bitwise AND (&)
The bitwise AND operator compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
Syntax
result = operand1 & operand2;
Example
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a & b; // Binary: 0001
Console.WriteLine("Bitwise AND: " + result);
Output:
Bitwise AND: 1
2. Bitwise OR (|)
The bitwise OR operator compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
Syntax
result = operand1 | operand2;
Example
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a | b; // Binary: 0111
Console.WriteLine("Bitwise OR: " + result);
Output:
Bitwise OR: 7
3. Bitwise XOR (^)
The bitwise XOR operator compares each bit of its first operand to the corresponding bit of its second operand. If the bits are different, the corresponding result bit is set to 1. If they are the same, the corresponding result bit is set to 0.
Syntax
result = operand1 ^ operand2;
Example
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a ^ b; // Binary: 0110
Console.WriteLine("Bitwise XOR: " + result);
Output:
Bitwise XOR: 6
4. Bitwise Complement (~)
The bitwise complement operator inverts the bits of its operand. Each 0 becomes 1 and each 1 becomes 0.
Syntax
result = ~operand;
Example
int a = 5; // Binary: 0101
int result = ~a; // Binary: 1010 (in 32-bit representation, this is actually -6 due to two's complement)
Console.WriteLine("Bitwise Complement: " + result);
Output:
Bitwise Complement: -6
5. Left Shift (<<)
The left shift operator shifts the bits of its first operand left by the number of positions specified by its second operand. Bits shifted off the left are discarded, and zero bits are shifted in from the right.
Syntax
result = operand1 << numberOfPositions;
Example
int a = 5; // Binary: 0101
int result = a << 1; // Binary: 1010 (equivalent to multiplying by 2)
Console.WriteLine("Left Shift: " + result);
Output:
Left Shift: 10
6. Right Shift (>>)
The right shift operator shifts the bits of its first operand right by the number of positions specified by its second operand. Bits shifted off the right are discarded, and the sign bit is shifted in from the left for signed types.
Syntax
result = operand1 >> numberOfPositions;
Example
int a = 5; // Binary: 0101
int result = a >> 1; // Binary: 0010 (equivalent to dividing by 2)
Console.WriteLine("Right Shift: " + result);
Output:
Right Shift: 2
Combining Bitwise Operators
Bitwise operators can be combined to perform more complex bitwise operations.
Example: Combining Bitwise AND, OR, and XOR
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int andResult = a & b; // Binary: 0001
int orResult = a | b; // Binary: 0111
int xorResult = a ^ b; // Binary: 0110
Console.WriteLine("Bitwise AND: " + andResult);
Console.WriteLine("Bitwise OR: " + orResult);
Console.WriteLine("Bitwise XOR: " + xorResult);
Output:
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Example: Using Bitwise Operators for Bit Manipulation
Bitwise operators are often used for tasks like setting, clearing, and toggling individual bits.
Set a Bit
To set a specific bit to 1, use the bitwise OR operator with a mask.
int num = 8; // Binary: 1000
int mask = 4; // Binary: 0100
int result = num | mask; // Binary: 1100
Console.WriteLine("Set Bit: " + result);
Output:
Set Bit: 12
Clear a Bit
To clear a specific bit to 0, use the bitwise AND operator with a mask.
int num = 8; // Binary: 1000
int mask = ~4; // Binary: 1011
int result = num & mask; // Binary: 1000
Console.WriteLine("Clear Bit: " + result);
Output:
Clear Bit: 8
Toggle a Bit
To toggle a specific bit, use the bitwise XOR operator with a mask.
int num = 8; // Binary: 1000
int mask = 4; // Binary: 0100
int result = num ^ mask; // Binary: 1100
Console.WriteLine("Toggle Bit: " + result);
Output:
Toggle Bit: 12
Conclusion
Bitwise operators are used for performing operations on individual bits of integers. They are essential in low-level programming, bit manipulation, and performance-critical applications. By understanding and using bitwise operators, you can perform efficient and effective bit-level operations in your C# programs.