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


C# BitArray.Xor方法代码示例

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


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

示例1: Main

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

   public static void Main()  {

      // Creates and initializes two BitArrays of the same size.
      BitArray myBA1 = new BitArray( 4 );
      BitArray myBA2 = new BitArray( 4 );
      myBA1[0] = myBA1[1] = false;
      myBA1[2] = myBA1[3] = true;
      myBA2[0] = myBA2[2] = false;
      myBA2[1] = myBA2[3] = true;

      // Performs a bitwise XOR operation between BitArray instances of the same size.
      Console.WriteLine( "Initial values" );
      Console.Write( "myBA1:" );
      PrintValues( myBA1, 8 );
      Console.Write( "myBA2:" );
      PrintValues( myBA2, 8 );
      Console.WriteLine();

      Console.WriteLine( "Result" );
      Console.Write( "XOR:" );
      PrintValues( myBA1.Xor( myBA2 ), 8 );
      Console.WriteLine();

      Console.WriteLine( "After XOR" );
      Console.Write( "myBA1:" );
      PrintValues( myBA1, 8 );
      Console.Write( "myBA2:" );
      PrintValues( myBA2, 8 );
      Console.WriteLine();

      // Performing XOR between BitArray instances of different sizes returns an exception.
      try  {
         BitArray myBA3 = new BitArray( 8 );
         myBA3[0] = myBA3[1] = myBA3[2] = myBA3[3] = false;
         myBA3[4] = myBA3[5] = myBA3[6] = myBA3[7] = true;
         myBA1.Xor( myBA3 );
      } catch ( Exception myException )  {
         Console.WriteLine("Exception: " + myException.ToString());
      }
   }

   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,代码行数:59,代码来源:BitArray.Xor

输出:

Initial values
myBA1:   False   False    True    True
myBA2:   False    True   False    True

Result
XOR:   False    True    True   False

After XOR
myBA1:   False    True    True   False
myBA2:   False    True   False    True

Exception: System.ArgumentException: Array lengths must be the same.
   at System.Collections.BitArray.Xor(BitArray value)
   at SamplesBitArray.Main()

示例2: showbits

//引入命名空间
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.Xor


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