C# Bitwise operators

C# Bitwise operators

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.

Types of Bitwise Operators in C#

Following are various types of Bitwise operators defined in C#:

  • Bitwise AND (&): 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.
  • Bitwise OR(|): 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.
  • Bitwise Exclusive OR (XOR^): 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.
  • Bitwise Left Shift (<<): It moves the number to the left, depending on the number of bits defined. The zeroes are appended to the smallest bits.
  • Bitwise Right Shift (>>): It moves the number to the right, depending on the number of bits defined. The zeroes are appended to the smallest bits.
  • Bitwise Complement (~): 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:

pqp & qp | qp ^ q
00000
01011
11110
10011

Assume if A = 60; and B = 13; then in the binary format they are as follows:

ABA&BA|BA^B~A
0011 11000000 11010000 11000011 11010011 00011100 0011

 

1 - Bitwise AND

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:

Bitwise AND

 

2 - Bitwise OR

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:

Bitwise OR

 

3 - Bitwise EXOR

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:

Bitwise EXOR

 

4 - Bitwise RightShift

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:

Bitwise RightShift

 

5 - Bitwise LeftShift

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 LeftShift

 

6 - Bitwise Complement

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:

Bitwise Complement

 

C# Bitwise Operators Example

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:

C# Bitwise Operators Example