获取或设置位数组中特定位置的位值 C#


要获取或设置位数组中特定位置的位值,代码如下 −

示例

 在线演示

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("BitArray1 length = "+arr1.Length);
      Console.WriteLine("BitArray1 first element = "+arr1.Get(0));
      arr2[0] = false;
      arr2[1] = true;
      Console.WriteLine("
BitArray2 length = "+arr2.Length);       Console.WriteLine("Elements in BitArray2...");       foreach (bool res in arr2){          Console.WriteLine(res);       }       Console.WriteLine("
Is BitArray1 equal to BitArray2? = "+arr2.Equals(arr1));    } }

输出

将产生以下输出 −

BitArray1 length = 2
BitArray1 first element = False BitArray2 length = 2
Elements in BitArray2...
False
True
Is BitArray1 equal to BitArray2? = False

示例

现在让我们看另一个示例 −

 在线演示

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("BitArray1 length = "+arr1.Length);
      Console.WriteLine("BitArray1 first element = "+arr1.Get(0));
      arr1[0] = true;
      Console.WriteLine("BitArray1 first element [UPDATED] = "+arr1.Get(0));
      arr2[0] = false;
      arr2[1] = true;
      Console.WriteLine("
BitArray2 length = "+arr2.Length);       Console.WriteLine("Elements in BitArray2...");       foreach (bool res in arr2){          Console.WriteLine(res);       }       Console.WriteLine("
Is BitArray1 equal to BitArray2? = "+arr2.Equals(arr1));    } }

输出

将产生以下输出 −

BitArray1 length = 2
BitArray1 first element = False BitArray1 first element [UPDATED] = True BitArray2 length = 2
Elements in BitArray2...
False
True
Is BitArray1 equal to BitArray2? = False

更新日期: 06-Dec-2019

91 浏览

开启你的职业生涯

完成教程后获得认证

开始吧
广告