本文整理汇总了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();
}
}
输出:
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);
}
}