Here we will learn bitwise operators in c# with examples, different types of bitwise operators (Bitwise AND, Bitwise OR, Bitwise XOR, Bitwise Complement, Bitwise Left Shift, Bitwise Right Shift) in c# with examples and how to use bitwise operators in c# to perform bit by bit operations on operands with examples.

In c#, **Bitwise Operators** will work on bits and these are useful to perform the bit by bit operations such as Bitwise AND (&), Bitwise OR (|), Bitwise Exclusive OR (^), etc. on operands and we can perform bit level operations on Boolean and integer data.

For example, we have an integer variables **a = 10**, **b = 20** and the binary format of these variables will be like as shown below.

a = 10 (00001010)

b = 20 (00010100)When we apply **Bitwise OR** (**|**) operator on these parameters we will get the result like as shown below.

00001010

00010100

-----------

00011110 = 30 (Decimal)Following table lists the different type of operators available in c# bitwise operators.

Operator | Name | Description | Example (a = 0, b = 1) |
---|---|---|---|

& | Bitwise AND | It compares each bit of first operand with the corresponding bit of its sencond operand. If both bits are 1, then the result bit will be 1 otherwise the result bit will be 0. | a & b (0) |

| | Bitwise OR | It compares each bit of first operand with the corresponding bit of its sencond operand. If either of bit is 1, then the result bit will be 1 otherwise the result bit will be 0. | a | b (1) |

^ | Bitwise Exclusive OR (XOR) | It compares each bit of first operand with the corresponding bit of its sencond operand. If one bit is 0 and other bit is 1, then the result bit will be 1 otherwise the result bit will be 0. | a ^ b (1) |

~ | Bitwise Complement | It operates on only one operand and it will invert each bit of operand. It will change bit 1 to 0 and vice versa. | ~(a) (1) |

<< | Bitwise Left Shift) | It shifts the number to the left based on the specified number of bits. The zeroes will be added to the least significant bits. | b << 2 (100) |

>> | Bitwise Right Shift | It shifts the number to the right based on the specified number of bits. The zeroes will be added to the least significant bits. | b >> 2 (001) |

Following is the example of using the Bitwise Operators in c# programming language.

using System;

namespace Tutlane

{

class Program

{

static void Main(string[] args)

{

int x = 5, y = 10, result;

result = x & y;

Console.WriteLine("Bitwise AND: " + result);

result = x | y;

Console.WriteLine("Bitwise OR: " + result);

result = x ^ y;

Console.WriteLine("Bitwise XOR: " + result);

result = ~x;

Console.WriteLine("Bitwise Complement: " + result);

result = x << 2;

Console.WriteLine("Bitwise Left Shift: " + result);

result = x >> 2;

Console.WriteLine("Bitwise Right Shift: " + result);

Console.WriteLine("Press Any Key to Exit..");

Console.ReadLine();

}

}

}

If you observe above code, we used a different bitwise operators (&, |, ^, ~, <<, >>) to perform different operations on defined operands.

When we execute above c# program, we will get the result like as shown below.

This is how we can use bitwise operators in c# programming language to perform bit by bit operations on defined operands based on our requirements.

Be the first to give your valuable feedback