Csharp/CSharp Tutorial/Data Structure/BitArray — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18:31, 26 мая 2010
Содержание
- 1 BitArray: Not()
- 2 BitArray: Xor()
- 3 Set the four elements of the BitArray and display the elements of the BitArray
- 4 Use copy constructor in BitArray
- 5 Use the Not() method to reverse the elements in BitArray
- 6 Use the Or() method to perform an OR operation on the elements in BitArray and another BitArray
BitArray: Not()
<source lang="csharp">using System; using System.Collections;
class MainClass {
public static void Main() { BitArray ba = new BitArray(8); byte[] b = { 67 }; BitArray ba2 = new BitArray(b); Console.WriteLine("Original contents of ba:"); for(int i=0; i < ba.Count; i++) Console.Write("{0, -6} ", ba[i]); ba = ba.Not(); Console.WriteLine("Contents of ba after Not:"); for(int i=0; i < ba.Count; i++) Console.Write("{0, -6} ", ba[i]); }
}</source>
Original contents of ba: False False False False False False False False Contents of ba after Not: True True True True True True True True
BitArray: Xor()
<source lang="csharp">using System; using System.Collections;
class MainClass {
public static void Main() { BitArray ba = new BitArray(8); byte[] b = { 67 }; BitArray ba2 = new BitArray(b); Console.WriteLine("Original contents of ba:"); for(int i=0; i < ba.Count; i++) Console.Write("{0, -6} ", ba[i]); ba = ba.Not(); BitArray ba3 = ba.Xor(ba2); Console.WriteLine("Result of ba XOR ba2:"); for(int i=0; i < ba3.Count; i++) Console.Write("{0, -6} ", ba3[i]); }
}</source>
Original contents of ba: False False False False False False False False Result of ba XOR ba2: False False True True True True False True
Set the four elements of the BitArray and display the elements of the BitArray
<source lang="csharp">using System; using System.Collections; class MainClass {
public static void Main() { BitArray myBitArray = new BitArray(4); Console.WriteLine("myBitArray.Length = " + myBitArray.Length); myBitArray[0] = false; myBitArray[1] = true; myBitArray[2] = true; myBitArray[3] = false; for (int i = 0; i < myBitArray.Count; i++) { Console.WriteLine("myBitArray[" + i + "] = " + myBitArray[i]); } }
}</source>
myBitArray.Length = 4 myBitArray[0] = False myBitArray[1] = True myBitArray[2] = True myBitArray[3] = False
Use copy constructor in BitArray
<source lang="csharp">using System; using System.Collections; class MainClass {
public static void DisplayBitArray(string arrayListName, BitArray myBitArray ) { for (int i = 0; i < myBitArray.Count; i++) { Console.WriteLine(arrayListName + "[" + i + "] = " + myBitArray[i]); } } public static void Main() { BitArray myBitArray = new BitArray(4); myBitArray[0] = false; myBitArray[1] = true; myBitArray[2] = true; myBitArray[3] = false; DisplayBitArray("myBitArray", myBitArray); BitArray anotherBitArray = new BitArray(myBitArray); DisplayBitArray("anotherBitArray", myBitArray); }
}</source>
myBitArray[0] = False myBitArray[1] = True myBitArray[2] = True myBitArray[3] = False anotherBitArray[0] = False anotherBitArray[1] = True anotherBitArray[2] = True anotherBitArray[3] = False
Use the Not() method to reverse the elements in BitArray
<source lang="csharp">using System; using System.Collections; class MainClass {
public static void DisplayBitArray(string arrayListName, BitArray myBitArray ) { for (int i = 0; i < myBitArray.Count; i++) { Console.WriteLine(arrayListName + "[" + i + "] = " + myBitArray[i]); } } public static void Main() { BitArray myBitArray = new BitArray(4); myBitArray[0] = false; myBitArray[1] = true; myBitArray[2] = true; myBitArray[3] = false; DisplayBitArray("myBitArray", myBitArray); myBitArray.Not(); DisplayBitArray("myBitArray", myBitArray); }
}</source>
myBitArray[0] = False myBitArray[1] = True myBitArray[2] = True myBitArray[3] = False myBitArray[0] = True myBitArray[1] = False myBitArray[2] = False myBitArray[3] = True
Use the Or() method to perform an OR operation on the elements in BitArray and another BitArray
<source lang="csharp">using System; using System.Collections; class MainClass {
public static void DisplayBitArray(string arrayListName, BitArray myBitArray ) { for (int i = 0; i < myBitArray.Count; i++) { Console.WriteLine(arrayListName + "[" + i + "] = " + myBitArray[i]); } } public static void Main() { BitArray myBitArray = new BitArray(4); myBitArray[0] = false; myBitArray[1] = true; myBitArray[2] = true; myBitArray[3] = false; DisplayBitArray("myBitArray", myBitArray); BitArray anotherBitArray = new BitArray(myBitArray); DisplayBitArray("anotherBitArray", myBitArray); myBitArray.Not(); DisplayBitArray("myBitArray", myBitArray); myBitArray.Or(anotherBitArray); DisplayBitArray("myBitArray", myBitArray);
}
}</source>
myBitArray[0] = False myBitArray[1] = True myBitArray[2] = True myBitArray[3] = False anotherBitArray[0] = False anotherBitArray[1] = True anotherBitArray[2] = True anotherBitArray[3] = False myBitArray[0] = True myBitArray[1] = False myBitArray[2] = False myBitArray[3] = True myBitArray[0] = True myBitArray[1] = True myBitArray[2] = True myBitArray[3] = True