Csharp/C Sharp/Collections Data Structure/BitArray

Материал из .Net Framework эксперт
Версия от 11:39, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Demonstrate BitArray

// Demonstrate BitArray. 
 
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); 
  } 
}


illustrates the use of a BitArray

/*
  illustrates the use of a BitArray
*/
using System;
using System.Collections;
public class Example11_4
{
  public static void Main()
  {
    // create a BitArray object
    BitArray myBitArray = new BitArray(4);
    // display the Length property
    Console.WriteLine("myBitArray.Length = " +
      myBitArray.Length);
    // set the four elements of the BitArray
    myBitArray[0] = false;
    myBitArray[1] = true;
    myBitArray[2] = true;
    myBitArray[3] = false;
    // display the elements of the BitArray
    for (int counter = 0; counter < myBitArray.Count; counter++)
    {
      Console.WriteLine("myBitArray[" + counter + "] = " +
        myBitArray[counter]);
    }
  }
}


illustrates the use of BitArray methods

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example11_5.cs illustrates the use of BitArray methods
*/
using System;
using System.Collections;
public class Example11_5
{
  // the DisplayBitArray() method displays the elements in the
  // supplied BitArray
  public static void DisplayBitArray(
    string arrayListName, BitArray myBitArray
  )
  {
    for (int counter = 0; counter < myBitArray.Count; counter++)
    {
      Console.WriteLine(arrayListName + "[" + counter + "] = " +
        myBitArray[counter]);
    }
  }
  public static void Main()
  {
    // create a BitArray object
    BitArray myBitArray = new BitArray(4);
    myBitArray[0] = false;
    myBitArray[1] = true;
    myBitArray[2] = true;
    myBitArray[3] = false;
    DisplayBitArray("myBitArray", myBitArray);
    // create another BitArray object, passing myBitArray to
    // the constructor
    BitArray anotherBitArray = new BitArray(myBitArray);
    DisplayBitArray("anotherBitArray", myBitArray);
    // use the Not() method to reverse the elements in myBitArray
    Console.WriteLine("Using the Not() method to reverse the element\n" +
      " values in myBitArray");
    myBitArray.Not();
    DisplayBitArray("myBitArray", myBitArray);
    // use the Or() method to perform an OR operation on the elements
    // in myBitArray and anotherBitArray
    Console.WriteLine("Using the Or() method to perform an OR operation on\n" +
      " the elements in myBitArray and anotherBitArray");
    myBitArray.Or(anotherBitArray);
    DisplayBitArray("myBitArray", myBitArray);
  }
}


Use BitArray collection as Flag

using System;
using System.Collections;
public class Starter {
    public static void Main() {
        Hashtable employees = new Hashtable();
        employees.Add("A100", new Employee("Ben", true, false, true));
        employees.Add("V100", new Employee("Valerie", false, false, true));
        Participation((Employee)employees["A100"]);
        Participation((Employee)employees["V100"]);
    }
    public static void Participation(Employee person) {
        Console.WriteLine(person.Name + ":");
        if (person.InProfitSharing) {
            Console.WriteLine(" Participating in Profit Sharing");
        }
        if (person.InHealthPlan) {
            Console.WriteLine(" Participating in Health Plan");
        }
        if (person.InCreditUnion) {
            Console.WriteLine(" Participating in Credit Union");
        }
    }
}
public class Employee {
    public Employee(string emplName) {
        propName = emplName;
        eflags.SetAll(true);
    }
    public Employee(string emplName,
                    bool profitSharing,
                    bool healthPlan,
                    bool creditUnion) {
        propName = emplName;
        InProfitSharing = profitSharing;
        InHealthPlan = healthPlan;
        InCreditUnion = creditUnion;
    }
    private BitArray eflags = new BitArray(3);
    public bool InProfitSharing {
        set {
            eflags.Set(0, value);
        }
        get {
            return eflags.Get(0);
        }
    }
    public bool InHealthPlan {
        set {
            eflags.Set(1, value);
        }
        get {
            return eflags.Get(1);
        }
    }
    public bool InCreditUnion {
        set {
            eflags.Set(2, value);
        }
        get {
            return eflags.Get(2);
        }
    }
    private string propName;
    public string Name {
        get {
            return propName;
        }
    }
}