In this tutorial, we will learn exhaustively about bitwise and bit shift operators in C#. C# provides 4 bitwise and 2 bit shift operators.

Bitwise operator works on bits and performs bit by bit operation.

Bitwise and bit shift operators are used to perform bit level procedure on integer (int, long, and so on) and boolean data. These operators are not generally used, in real life, circumstances.

On the off chance that you are intrigued to explore more, visit practical applications of bitwise operations.

The bitwise and bit shift operators accessible in C# are listed underneath.

List of C# Bitwise Operators

OperatorOperator Name
~Bitwise Complement
&Bitwise AND
|Bitwise OR
^Bitwise Exclusive OR (XOR)
<<Bitwise Left Shift
>>Bitwise Right Shift

Bitwise OR

Bitwise OR operator is addressed by |. It performs bitwise OR procedure on the corresponding bits of two operands. On the off chance that both of the bits is 1, the result is 1. In any case the outcome is 0.

In the event that the operands are of type bool, the bitwise OR activity is identical to legitimate OR activity between them.

For Example,

14 = 00001110 (In Binary)
11 = 00001011 (In Binary)

Bitwise OR operation somewhere in the range of 14 and 11:

00001110
00001011
--------
00001111 = 15 (In Decimal)

Example 1: Bitwise OR

using System;
 
namespace Operator
{
	class BitWiseOR
	{
		public static void Main(string[] args)
		{
			int firstNumber = 14, secondNumber = 11, result;
			result = firstNumber | secondNumber;
			Console.WriteLine("{0} | {1} = {2}", firstNumber, secondNumber, result);
		}
	}
}

At the point when we run the program, the output will be:

14 | 11 = 15

Bitwise AND

Bitwise AND operator is addressed by and. It performs bitwise AND operation on the corresponding bits of two operands. On the off chance that either of the bits is 0, the outcome is 0. In any case the outcome is 1.

On the off chance that the operands are of type bool, the bitwise AND operation is comparable to logical AND operation between them.

For Example,

14 = 00001110 (In Binary)
11 = 00001011 (In Binary)

Bitwise AND operation somewhere in the range of 14 and 11:

00001110
00001011
--------
00001010 = 10 (In Decimal)

Example 2: Bitwise AND

using System;
 
namespace Operator
{
	class BitWiseAND
	{
		public static void Main(string[] args)
		{
			int firstNumber = 14, secondNumber = 11, result;
			result = firstNumber & secondNumber;
			Console.WriteLine("{0} & {1} = {2}", firstNumber, secondNumber, result);
		}
	}
}

At the point when we run the program, the output will be:

14 & 11 = 10

Bitwise XOR

Bitwise XOR operation is addressed by ^. It performs bitwise XOR operation on the corresponding bits of two operands. On the off chance that the corresponding bits are same, the outcome is 0. On the off chance that the corresponding bits are different, the outcome is 1.

In the event that the operands are of type bool, the bitwise XOR operation is identical to logical XOR operation between them.

For Example,

14 = 00001110 (In Binary)
11 = 00001011 (In Binary)

Bitwise XOR operation somewhere in the range of 14 and 11:

00001110
00001011
--------
00000101 = 5 (In Decimal)

Example 3: Bitwise XOR

using System;
 
namespace Operator
{
	class BitWiseXOR
	{
		public static void Main(string[] args)
		{
			int firstNumber = 14, secondNumber = 11, result;
			result = firstNumber^secondNumber;
			Console.WriteLine("{0} ^ {1} = {2}", firstNumber, secondNumber, result);
		}
	}
}

At the point when we run the program, the output will be:

14 ^ 11 = 5

Bitwise Complement

Bitwise Complement operator is addressed by ~. It is an unary operator, for example works on just a single operand. The ~ operator inverts each bits for example changes 1 to 0 and 0 to 1.

For Example,

26 = 00011010 (In Binary)

Bitwise Complement operation on 26:

~ 00011010 = 11100101 = 229 (In Decimal)

Example 4: Bitwise Complement

using System;
 
namespace Operator
{
	class BitWiseComplement
	{
		public static void Main(string[] args)
		{
			int number = 26, result;
			result = ~number;
			Console.WriteLine("~{0} = {1}", number, result);
		}
	}
}

At the point when we run the program, the output will be:

~26 = -27

We got – 27 as output when we were expecting 229. For what reason did this occur?

It happens because the binary value 11100101 which we expect to be 229 is actually a 2’s complement representation of – 27. Negative numbers in computer are addressed in 2’s complement representation.

For any integer n, 2’s complement of n will be – (n+1).

DecimalBinary2’s Complement
000000000-(11111111 + 1) = -00000000 = -0 (In Decimal)
100000001-(11111110 + 1) = -11111111 = -256 (In Decimal)
22911100101-(00011010 + 1) = -00011011 = -27

Overflow values are ignored in 2’s complement.

The bitwise complement of 26 is 229 (in decimal) and the 2’s complement of 229 is – 27. Henceforth the output is – 27 rather than 229.


Bitwise Left Shift

Bitwise left shift operator is addressed by <<. The << operator shifts a number to the left by a predefined number of bits. Zeroes are added to the least significant bits.

In decimal, it is comparable to

num * 2bits

For Example,

42 = 101010 (In Binary)

Bitwise Lift Shift procedure on 42:

42 << 1 = 84 (In binary 1010100)
42 << 2 = 168 (In binary 10101000)
42 << 4 = 672 (In binary 1010100000)

Example 5: Bitwise Left Shift

using System;
 
namespace Operator
{
	class LeftShift
	{
		public static void Main(string[] args)
		{
			int number = 42;

			Console.WriteLine("{0}<<1 = {1}", number, number<<1);
			Console.WriteLine("{0}<<2 = {1}", number, number<<2);
			Console.WriteLine("{0}<<4 = {1}", number, number<<4);
		}
	}
}

At the point when we run the program, the output will be:

42<<1 = 84
42<<2 = 168
42<<4 = 672

Bitwise Right Shift

Bitwise left shift operator is addressed by >>. The >> operator shifts a number to the right by a predetermined number of bits. The first operand is shifted to right by the number of bits indicated by second operand.

In decimal, it is identical to

floor(num / 2bits)

For Example,

42 = 101010 (In Binary)

Bitwise Lift Shift operation on 42:

42 >> 1 = 21 (In binary 010101)
42 >> 2 = 10 (In binary 001010)
42 >> 4 = 2 (In binary 000010)

Example 6: Bitwise Right Shift

using System;
 
namespace Operator
{
	class LeftShift
	{
		public static void Main(string[] args)
		{
			int number = 42;

			Console.WriteLine("{0}>>1 = {1}", number, number>>1);
			Console.WriteLine("{0}>>2 = {1}", number, number>>2);
			Console.WriteLine("{0}>>4 = {1}", number, number>>4);
		}
	}
}

At the point when we run the program, the output will be:

42>>1 = 21
42>>2 = 10
42>>4 = 2

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.