当前位置: 首页>>代码示例>>C#>>正文


C# BitArray类代码示例

本文整理汇总了C#中System.Collections.BitArray的典型用法代码示例。如果您正苦于以下问题:C# BitArray类的具体用法?C# BitArray怎么用?C# BitArray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


BitArray类属于System.Collections命名空间,在下文中一共展示了BitArray类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Collections;
public class SamplesBitArray  {

   public static void Main()  {

      // Creates and initializes several BitArrays.
      BitArray myBA1 = new BitArray( 5 );

      BitArray myBA2 = new BitArray( 5, false );

      byte[] myBytes = new byte[5] { 1, 2, 3, 4, 5 };
      BitArray myBA3 = new BitArray( myBytes );

      bool[] myBools = new bool[5] { true, false, true, true, false };
      BitArray myBA4 = new BitArray( myBools );

      int[]  myInts  = new int[5] { 6, 7, 8, 9, 10 };
      BitArray myBA5 = new BitArray( myInts );

      // Displays the properties and values of the BitArrays.
      Console.WriteLine( "myBA1" );
      Console.WriteLine( "   Count:    {0}", myBA1.Count );
      Console.WriteLine( "   Length:   {0}", myBA1.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA1, 8 );

      Console.WriteLine( "myBA2" );
      Console.WriteLine( "   Count:    {0}", myBA2.Count );
      Console.WriteLine( "   Length:   {0}", myBA2.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA2, 8 );

      Console.WriteLine( "myBA3" );
      Console.WriteLine( "   Count:    {0}", myBA3.Count );
      Console.WriteLine( "   Length:   {0}", myBA3.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA3, 8 );

      Console.WriteLine( "myBA4" );
      Console.WriteLine( "   Count:    {0}", myBA4.Count );
      Console.WriteLine( "   Length:   {0}", myBA4.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA4, 8 );

      Console.WriteLine( "myBA5" );
      Console.WriteLine( "   Count:    {0}", myBA5.Count );
      Console.WriteLine( "   Length:   {0}", myBA5.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA5, 8 );
   }

   public static void PrintValues( IEnumerable myList, int myWidth )  {
      int i = myWidth;
      foreach ( Object obj in myList ) {
         if ( i <= 0 )  {
            i = myWidth;
            Console.WriteLine();
         }
         i--;
         Console.Write( "{0,8}", obj );
      }
      Console.WriteLine();
   }
}
开发者ID:.NET开发者,项目名称:System.Collections,代码行数:66,代码来源:BitArray

输出:

myBA1
   Count:    5
   Length:   5
   Values:
   False   False   False   False   False
myBA2
   Count:    5
   Length:   5
   Values:
   False   False   False   False   False
myBA3
   Count:    40
   Length:   40
   Values:
    True   False   False   False   False   False   False   False
   False    True   False   False   False   False   False   False
    True    True   False   False   False   False   False   False
   False   False    True   False   False   False   False   False
    True   False    True   False   False   False   False   False
myBA4
   Count:    5
   Length:   5
   Values:
    True   False    True    True   False
myBA5
   Count:    160
   Length:   160
   Values:
   False    True    True   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
    True    True    True   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False    True   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
    True   False   False    True   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False    True   False    True   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False

示例2: new BitArray(byte[] b)

//引入命名空间
using System; 
using System.Collections; 
 
public class BADemo { 
  public static void showbits(string rem, 
                         BitArray bits) { 
    Console.WriteLine(rem); 
    for(int i=0; i < bits.Count; i++) 
      Console.Write("{0, -6} ", bits[i]); 
    Console.WriteLine("\n"); 
  } 
 
  public static void Main() { 
    BitArray ba = new BitArray(8); 
    byte[] b = { 67 }; 
    BitArray ba2 = new BitArray(b); 
     
    showbits("Original contents of ba:", ba); 
       
    ba = ba.Not(); 
 
    showbits("Contents of ba after Not:", ba); 
 
    showbits("Contents of ba2:", ba2); 
 
    BitArray ba3 = ba.Xor(ba2); 
 
    showbits("Result of ba XOR ba2:", ba3); 
  } 
}
开发者ID:C#程序员,项目名称:System.Collections,代码行数:31,代码来源:BitArray


注:本文中的System.Collections.BitArray类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。