Bitwise operators manipulate the individual bits of integer values. They work directly on the binary representation of numbers. Here’s a table summarizing the operators:
Operator | Name | Description |
& | Bitwise AND | Performs a bitwise AND operation. If both corresponding bits are 1, the resulting bit is 1; otherwise, it’s 0. |
`\ | ` | Bitwise OR |
^ | Bitwise XOR | Performs a bitwise exclusive OR (XOR) operation. If the corresponding bits are different, the resulting bit is 1; otherwise, it’s 0. |
~ | Bitwise NOT | Performs a bitwise complement (NOT) operation, flipping all the bits. |
<< | Left Shift | Shifts the bits of a value to the left by a specified number of positions, filling vacated positions with 0s. |
>> | Right Shift | Shifts the bits of a value to the right by a specified number of positions. |
- Bitwise AND (
&
)
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a & b; // Binary: 0001 (Decimal: 1)
Console.WriteLine(result); // Output: 1
Explanation:
- For each corresponding bit position, the
&
operator produces a1
only if both bits are1
. 0101 & 0011
results in0001
.
Bitwise OR ( |
)
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a | b; // Binary: 0111 (Decimal: 7)
Console.WriteLine(result); // Output: 7
Explanation:
- The
|
operator produces a1
if at least one of the corresponding bits is1
. 0101 | 0011
results in0111
.
Bitwise XOR ( ^
)
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a ^ b; // Binary: 0110 (Decimal: 6)
Console.WriteLine(result); // Output: 6
Explanation:
- The
^
operator produces a1
if the corresponding bits are different. 0101 ^ 0011
results in0110
.
Bitwise NOT ( ~
)
int a = 5; // Binary 0000 0101 (32 bit Integer)
int result = ~a; //Binary 1111 1010. Decimal -6
Console.WriteLine(result); //output -6.
Explanation
- The ~ operator inverts all the bits. All 0’s become 1’s, and all 1’s become 0’s.
- Due to the nature of 2’s complement in how negative numbers are handled, the output of ~5 is -6.
Left Shift ( <<
)
int a = 5; // Binary: 0000 0101
int result = a << 2; // Binary: 0001 0100 (Decimal: 20)
Console.WriteLine(result); // Output: 20
Explanation:
- The
<<
operator shifts the bits ofa
two positions to the left. - This effectively multiplies
a
by 2 raised to the power of the shift amount (2<sup>2</sup> = 4). 5 * 4 = 20
.
Right Shift ( >>
)
int a = 20; // Binary: 0001 0100
int result = a >> 2; // Binary: 0000 0101 (Decimal: 5)
Console.WriteLine(result); // Output: 5
Explanation:
- The
>>
operator shifts the bits ofa
two positions to the right. - This effectively divides
a
by 2 raised to the power of the shift amount. - Integer division of 20/4 equals 5.
Common Use Cases
- Flags and Bitmasks: Bitwise operations are commonly used to represent and manipulate sets of boolean flags. Each bit represents a different state.
- Low-Level Hardware Interaction: When dealing with hardware, bitwise operators are essential for working with registers and memory-mapped I/O.
- Efficient Calculations: Bit shifting can be a fast way to multiply or divide by powers of 2.
- Cryptography: Bitwise operations are used extensively in cryptographic algorithms.
- Image Processing: image manipulation often involves bitwise operations on pixel color values.
Key Points
- Bitwise operators work on integers (
int
,uint
,long
,ulong
,byte
,sbyte
,short
,ushort
). - Understand binary representation to fully grasp how these operators work.
- Bit shifting can significantly impact the value of a number.
COMMENTS