In C#, there are 6 bitwise operators which work at bit level or used to perform bit by bit operations.
In C#, Bitwise Operators will work on bits, and these are useful to perform bit by bit operations such as Bitwise AND (&
), Bitwise OR (|
), Bitwise Exclusive OR (^
), etc. on operands. We can perform bit-level operations on Boolean and integer data.
Following are various types of Bitwise operators defined in C#:
&
): Each bit from the first operand is associated with that of its second operand. When both bits are 1 then the result bit is 1 if not 0.|
): Each bit from the first operand is associated with that of its second operand. If either of the bit is 1 then the result bit is 1 if not 0.^
): Every bit from the first operand is comparable to its second operand’s subsequent bit. When one bit is 0 and the other is 1 the result bit is 1 if not the result bit is 0.<<
): It moves the number to the left, depending on the number of bits defined. The zeroes are appended to the smallest bits.>>
): It moves the number to the right, depending on the number of bits defined. The zeroes are appended to the smallest bits.~
): Bitwise complement operator is a unary operator that operates on one operand only. The ~
operator switches from 1 to 0 and from 0 to 1.
The truth tables for &
, |
, and ^
are as follows:
p | q | p & q | p | q | p ^ q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Assume if A = 60;
and B = 13;
then in the binary format they are as follows:
A | B | A&B | A|B | A^B | ~A |
---|---|---|---|---|---|
0011 1100 | 0000 1101 | 0000 1100 | 0011 1101 | 0011 0001 | 1100 0011 |
It only gives True
while using AND operation if both values are True
. This operator can be implemented by using ‘&
’ operator.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
byte A = 10;// This binary is equivalent for 10 is 01010
byte B = 20;// This binary is equivalent for 20 is 10100
long myresult = A & B; // The result of AND operation result is: 00000
Console.WriteLine("{0} AND {1} result is :{2}", A, B, myresult);
A = 10;// This binary is equivalent for 10 is 01010
B = 10;// This binary is equivalent for 10 is 01010
myresult = A & B; // The result of AND operation result is: 01010
Console.WriteLine("{0} AND {1} result is : {2}", A, B, myresult);
Console.ReadLine();
}
}
}
Output:
It only provides FALSE
while using the OR method if both the values are FALSE
. OR operation is true
in all other cases. This operator can be implemented by using the ‘|
’ operator.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
byte A = 10;// This binary is equivalent for 10 is 01010
byte B = 20;// This binary is equivalent for 20 is 10100
long myresult = A | B; // The result of AND operation result is: 00000
Console.WriteLine("{0} OR {1} result is :{2}", A, B, myresult);
A = 10;// This binary is equivalent for 10 is 01010
B = 10;// This binary is equivalent for 10 is 01010
myresult = A | B; // The result of AND operation result is: 01010
Console.WriteLine("{0} OR {1} result is : {2}", A, B, myresult);
Console.ReadLine();
}
}
}
Output:
If the related bits are unique, then this gives 1, otherwise 0. This operator can be implemented by using the ‘^
’ operator.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int num1 = 14, num2 = 11, result;
result = num1 ^ num2;
Console.WriteLine("{0} ^ {1} = {2}", num1, num2, result);
Console.ReadLine();
}
}
}
Output:
If RightShift operations are performed with a binary value, the bits will be shifted to one location on the right side. This operator can be implemented by using ‘>>
’ operator.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
byte varA = 10;// This binary is equivalent for 10 is 01010
long myresult = varA >> 1; // The right shift operation result is : 0101
Console.WriteLine("{0} is right shifted to 1 position result is:{1}", varA, myresult);
Console.ReadLine();
}
}
}
Output:
If LeftShift operations are performed with a binary value, the bits will be shifted to one location on the left side. This operator can be implemented by using the ‘<<
’ operator.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
byte varA = 10;// This binary is equivalent for 10 is 01010
long myresult = varA << 1; // The left shift operation result is : 10100
Console.WriteLine("{0} is left shifted to 1 position result is:{1}", varA, myresult);
Console.ReadLine();
}
}
}
Output:
Bitwise complement operator is specified by the ‘~
’ which is a unary operator that operates on one operand only. The ~
operator inverts a bit, i.e. switches from 1 to 0 and from 0 to 1.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int num = 22, num_result;
num_result = ~num;
Console.WriteLine("~{0} = {1}", num, num_result);
Console.ReadLine();
}
}
}
Output:
Following is the example of using the Bitwise Operators in the c# programming language:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int x = 5, y = 10, result;
// Bitwise AND Operator
result = x & y;
Console.WriteLine("Bitwise AND: " + result);
// Bitwise OR Operator
result = x | y;
Console.WriteLine("Bitwise OR: " + result);
// Bitwise XOR Operator
result = x ^ y;
Console.WriteLine("Bitwise XOR: " + result);
// Bitwise AND Operator
result = ~x;
Console.WriteLine("Bitwise Complement: " + result);
// Bitwise LEFT SHIFT Operator
result = x << 2;
Console.WriteLine("Bitwise Left Shift: " + result);
// Bitwise RIGHT SHIFT Operator
result = x >> 2;
Console.WriteLine("Bitwise Right Shift: " + result);
Console.ReadLine();
}
}
}
Output: