Algorithm
Bitwise Operators:
-
AND Operator (&):
- Performs a bitwise AND operation.
- Example:
result = a & b;
-
OR Operator (|):
- Performs a bitwise OR operation.
- Example:
result = a | b;
-
XOR Operator (^):
- Performs a bitwise exclusive OR operation.
- Example:
result = a ^ b;
-
NOT Operator (~):
- Performs a bitwise NOT operation, inverting each bit.
- Example:
result = ~a;
Bit Shift Operators:
-
Left Shift Operator (<<):
- Shifts the bits of the left operand to the left by a specified number of positions.
- Example:
result = a << 2;
(Left shifta
by 2 positions.)
-
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 shifta
by 3 positions.)
Common Bitwise Techniques:
-
Setting a Bit:
- Use the OR operator to set a specific bit to 1.
- Example:
a = a | (1 << position);
-
Clearing a Bit:
- Use the AND operator with the bitwise NOT operator to clear a specific bit to 0.
- Example:
a = a & ~(1 << position);
-
Toggling a Bit:
- Use the XOR operator to toggle a specific bit.
- Example:
a = a ^ (1 << position);
-
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 */ }
-
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
#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
#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
#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
#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
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
42>>2 = 10
42>>4 = 2
Demonstration
C# Programing Bitwise and Bit Shift Operators