Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Bitwise OR operation between the elements of BitArray in C#
The Bitwise OR operation between elements of BitArray in C# is performed using the Or() method. This method performs a logical OR operation on corresponding bits of two BitArray objects, returning true when at least one of the corresponding bits is true.
Syntax
Following is the syntax for performing Bitwise OR operation on BitArray −
BitArray result = bitArray1.Or(bitArray2);
Parameters
bitArray2 − The BitArray with which to perform the bitwise OR operation.
Return Value
The method returns the current BitArray instance containing the result of the bitwise OR operation. The original BitArray is modified in place.
Example
Let us see how to implement Bitwise OR operation between the elements of BitArray −
using System;
using System.Collections;
public class Demo {
public static void Main() {
BitArray arr1 = new BitArray(5);
BitArray arr2 = new BitArray(5);
arr1[0] = false;
arr1[1] = false;
arr1[2] = true;
arr1[3] = false;
arr1[4] = true;
arr2[0] = false;
arr2[1] = true;
arr2[2] = false;
arr2[3] = false;
arr2[4] = true;
Console.WriteLine("BitArray1 elements...");
foreach (bool res in arr1) {
Console.WriteLine(res);
}
Console.WriteLine("\nBitArray2 elements...");
foreach (bool res in arr2) {
Console.WriteLine(res);
}
Console.WriteLine("\nBitwise OR operation...");
BitArray result = arr1.Or(arr2);
foreach(bool bit in result) {
Console.WriteLine(bit);
}
}
}
The output of the above code is −
BitArray1 elements... False False True False True BitArray2 elements... False True False False True Bitwise OR operation... False True True False True
Using Or() with Different Patterns
Example
using System;
using System.Collections;
public class BitwiseOrDemo {
public static void Main() {
// Create BitArrays with alternating patterns
BitArray pattern1 = new BitArray(new bool[] {true, false, true, false});
BitArray pattern2 = new BitArray(new bool[] {false, true, false, true});
Console.WriteLine("Pattern 1: T F T F");
Console.WriteLine("Pattern 2: F T F T");
BitArray orResult = pattern1.Or(pattern2);
Console.Write("OR Result: ");
foreach(bool bit in orResult) {
Console.Write(bit ? "T " : "F ");
}
Console.WriteLine();
// Demonstrate that original BitArray is modified
Console.WriteLine("\nOriginal pattern1 after Or() operation:");
Console.Write("Pattern 1: ");
foreach(bool bit in pattern1) {
Console.Write(bit ? "T " : "F ");
}
Console.WriteLine();
}
}
The output of the above code is −
Pattern 1: T F T F Pattern 2: F T F T OR Result: T T T T Original pattern1 after Or() operation: Pattern 1: T T T T
Conclusion
The Or() method in C# BitArray performs bitwise OR operation between corresponding elements of two BitArrays. The result is true when at least one of the corresponding bits is true, and the operation modifies the original BitArray in place.
