
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
BitArray Class in C#
The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0).
The following table lists some of the commonly used methods of the BitArray class −
Sr.No. | Method & Description |
---|---|
1 | public BitArray And(BitArray value); Performs the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray. |
2 | public bool Get(int index); Gets the value of the bit at a specific position in the BitArray. |
3 | public BitArray Not(); Inverts all the bit values in the current BitArray, so that elements set to true are changed to false, and elements set to false are changed to true. |
4 | public BitArray Or(BitArray value); Performs the bitwise OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray. |
5 | public void Set(int index, bool value); Sets the bit at a specific position in the BitArray to the specified value. |
6 | public void SetAll(bool value); Sets all bits in the BitArray to the specified value. |
7 | public BitArray Xor(BitArray value); Performs the bitwise exclusive OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray. |
Example
Let us now see an example −
using System; using System.Collections; public class Demo { public static void Main(){ BitArray arr1 = new BitArray(2); BitArray arr2 = new BitArray(2); arr1[0] = false; arr1[1] = true; Console.WriteLine("Elements in BitArray1..."); foreach (bool res in arr1){ Console.WriteLine(res); } arr2[0] = false; arr2[1] = true; Console.WriteLine("Elements in BitArray2..."); foreach (bool res in arr2){ Console.WriteLine(res); } Console.WriteLine("Is BitArray1 equal to BitArray2? = "+arr2.Equals(arr1)); Console.WriteLine("Is BitArray synchronized? = "+arr2.IsSynchronized); Console.WriteLine("Is BitArray read-only? = "+arr2.IsReadOnly); } }
Output
This will produce the following output −
Elements in BitArray1... False True Elements in BitArray2... False True Is BitArray1 equal to BitArray2? = False Is BitArray synchronized? = False Is BitArray read-only? = False
Example
Let us see another example to implement Bitwise exclusive OR operation between 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; arr2[0] = false; arr2[1] = true; Console.WriteLine("BitArray1 elements..."); foreach (bool res in arr1){ Console.WriteLine(res); } Console.WriteLine("
BitArray2 elements..."); foreach (bool res in arr2){ Console.WriteLine(res); } Console.WriteLine("
Bitwise exclusive OR operation..."); IEnumerable demoEnum = arr1.Xor(arr2); foreach(Object ob in demoEnum){ Console.WriteLine(ob); } } }
Output
This will produce the following output −
BitArray1 elements... False False False False False BitArray2 elements... False True False False False Bitwise exclusive OR operation... False True False False False
- Related Articles
- What is the BitArray class in C#?
- What is the Count property of BitArray class in C#?
- What is the Item property of BitArray class in C#?
- What is the IsReadOnly property of BitArray class in C#?
- What is the Length property of BitArray class in C#?
- Inverting all bit values in BitArray in C#
- Copying BitArray elements to an Array in C#
- Enumerator that iterates through the BitArray in C#
- Number of elements contained in the BitArray in C#?
- Check if the BitArray is read-only in C#
- Check if two BitArray objects are equal in C#
- What is index-based I/O BitArray collection in C#?
- Bitwise OR operation between the elements of BitArray in C#
- Bitwise exclusive OR operation between the elements of BitArray in C#
- How to create a shallow copy of the BitArray in C#?

Advertisements