Algorithm


Bitwise Operators:

  1. AND Operator (&):

    • Performs a bitwise AND operation.
    • Example: result = a & b;
  2. OR Operator (|):

    • Performs a bitwise OR operation.
    • Example: result = a | b;
  3. XOR Operator (^):

    • Performs a bitwise exclusive OR operation.
    • Example: result = a ^ b;
  4. NOT Operator (~):

    • Performs a bitwise NOT operation, inverting each bit.
    • Example: result = ~a;

Bit Shift Operators:

  1. Left Shift Operator (<<):

    • Shifts the bits of the left operand to the left by a specified number of positions.
    • Example: result = a << 2; (Left shift a by 2 positions.)
  2. Right Shift Operator (>>):

    • Shifts the bits of the left operand to the right by a specified number of positions.
    • For unsigned types, it fills the vacant positions with zeros.
    • For signed types, it fills the vacant positions with the sign bit (0 for positive, 1 for negative).
    • Example: result = a >> 3; (Right shift a by 3 positions.)

Common Bitwise Techniques:

  1. Setting a Bit:

    • Use the OR operator to set a specific bit to 1.
    • Example: a = a | (1 << position);
  2. Clearing a Bit:

    • Use the AND operator with the bitwise NOT operator to clear a specific bit to 0.
    • Example: a = a & ~(1 << position);
  3. Toggling a Bit:

    • Use the XOR operator to toggle a specific bit.
    • Example: a = a ^ (1 << position);
  4. Checking if a Bit is Set:

    • Use the AND operator to check if a specific bit is set.
    • Example: if ((a & (1 << position)) != 0) { /* Bit is set */ }
  5. Checking if a Bit is Clear:

    • Use the AND operator to check if a specific bit is clear.
    • Example: if ((a & (1 << position)) == 0) { /* Bit is clear */ }

 

Code Examples

#1 Bitwise OR in C# Programing

Code - C# Programming

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);
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
14 | 11 = 15

#2 Bitwise AND in C# Programming

Code - C# Programming

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);
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
14 & 11 = 10

#3 Bitwise XOR in C# Programming

Code - C# Programming

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);
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
14 ^ 11 = 5

#4 Bitwise Complement in C# Programming

Code - C# Programming

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

Output

x
+
cmd
~26 = -27

#5 Bitwise Left Shift in C# Programming

Code - C# Programming

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);
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
42<<1 = 84
42<<2 = 168
42<<4 = 672

#6 Bitwise Right Shift in C# Programming

Code - C# Programming

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);
		}
	}
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
42>>1 = 21
42>>2 = 10
42>>4 = 2
Advertisements

Demonstration


C# Programing Bitwise and Bit Shift Operators