Latest web development tutorials

C # dot arrays (BitArray)

C # set C # set

BitArray class manages a compact array of bit values, which uses a Boolean value, where true indicates a bit is on (1), false representation bit is off (0).

When you need to store the bit, but I do not know in advance the number of bits, using an array of dots. You can usean integer index to access the array from the collection point, the index from scratch.

Class methods and properties BitArray

The following table lists some of the commonattributes BitArrayclass:

属性描述
Count获取 BitArray 中包含的元素个数。
IsReadOnly获取一个值,表示 BitArray 是否只读。
Item获取或设置 BitArray 中指定位置的位的值。
Length获取或设置 BitArray 中的元素个数。

The following table lists some of the commonmethods BitArrayclass:

序号方法名 & 描述
1public BitArray And( BitArray value );
对当前的 BitArray 中的元素和指定的 BitArray 中的相对应的元素执行按位与操作。
2public bool Get( int index );
获取 BitArray 中指定位置的位的值。
3public BitArray Not();
把当前的 BitArray 中的位值反转,以便设置为 true 的元素变为 false,设置为 false 的元素变为 true。
4public BitArray Or( BitArray value );
对当前的 BitArray 中的元素和指定的 BitArray 中的相对应的元素执行按位或操作。
5public void Set( int index, bool value );
把 BitArray 中指定位置的位设置为指定的值。
6public void SetAll( bool value );
把 BitArray 中的所有位设置为指定的值。
7public BitArray Xor( BitArray value );
对当前的 BitArray 中的元素和指定的 BitArray 中的相对应的元素执行按位异或操作。

Examples

The following example illustrates the point array (BitArray) use:

using System;
using System.Collections;

namespace CollectionsApplication
{
    class Program
    {
        static void Main (string [] args)
        {
            // Create two arrays of size 8 points BitArray ba1 = new BitArray (8);
            BitArray ba2 = new BitArray (8);
            byte [] a = {60};
            byte [] b = {13};
            
            // The value of 60 and 13 stores a point array ba1 = new BitArray (a);
            ba2 = new BitArray (b);

            // Ba1 content of Console.WriteLine ( "Bit array ba1: 60");
            for (int i = 0; i <ba1.Count; i ++)
            {
                Console.Write ( "{0, -6}", ba1 [i]);
            }
            Console.WriteLine ();
            
            // Ba2 content of Console.WriteLine ( "Bit array ba2: 13");
            for (int i = 0; i <ba2.Count; i ++)
            {
                Console.Write ( "{0, -6}", ba2 [i]);
            }
            Console.WriteLine ();
           
            
            BitArray ba3 = new BitArray (8);
            ba3 = ba1.And (ba2);

            // Ba3 content of Console.WriteLine ( "Bit array ba3 after AND operation: 12");
            for (int i = 0; i <ba3.Count; i ++)
            {
                Console.Write ( "{0, -6}", ba3 [i]);
            }
            Console.WriteLine ();

            ba3 = ba1.Or (ba2);
            // Ba3 content of Console.WriteLine ( "Bit array ba3 after OR operation: 61");
            for (int i = 0; i <ba3.Count; i ++)
            {
                Console.Write ( "{0, -6}", ba3 [i]);
            }
            Console.WriteLine ();
            
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Bit array ba1: 60 
False False True True True True False False 
Bit array ba2: 13
True False True True False False False False 
Bit array ba3 after AND operation: 12
False False True True False False False False 
Bit array ba3 after OR operation: 61
True False True True False False False False 

C # set C # set