sashomaga

BitArray 64bit

Mar 11th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. class BitArray64 : IEnumerable<int>
  2. {
  3.     private ulong bits;
  4.     private const int Max = 63;
  5.  
  6.     public BitArray64(ulong bits)
  7.     {
  8.         this.bits = bits;
  9.     }
  10.  
  11.     public int this [int index]
  12.     {
  13.         get
  14.         {
  15.             if (index < 0 || index > Max)
  16.             {
  17.                 throw new IndexOutOfRangeException("Index must be >= 0 and <= 63");
  18.             }
  19.             if ( (bits & (ulong)1 << index  ) == 0)
  20.             {
  21.                 return 0;
  22.             }
  23.             else
  24.             {
  25.                 return 1;
  26.             }          
  27.         }
  28.         set
  29.         {
  30.             if (index < 0 || index > Max)
  31.             {
  32.                 throw new IndexOutOfRangeException("Index must be >= 0 and <= 63");
  33.             }
  34.             if (value != 0 && value != 1)
  35.             {
  36.                 throw new ArgumentException("Value must be 1 or 0");
  37.             }
  38.  
  39.             if (value == 1)
  40.             {
  41.                 this.bits = this.bits | ((ulong)1 << index);
  42.             }
  43.             else
  44.             {
  45.                 this.bits = this.bits & (~((ulong)1 << index));
  46.             }
  47.         }
  48.     }
  49.  
  50.     public override int GetHashCode()
  51.     {
  52.         return this.GetHashCode();
  53.     }
  54.  
  55.  
  56.     public static bool operator ==(BitArray64 bit1, BitArray64 bit2)
  57.     {
  58.         return (BitArray64.Equals(bit1, bit2));
  59.     }
  60.     public static bool operator !=(BitArray64 bit1, BitArray64 bit2)
  61.     {
  62.         return !(BitArray64.Equals(bit1, bit2));
  63.     }
  64.  
  65.     public override bool Equals(object obj)
  66.     {
  67.         if (obj == null)
  68.         {
  69.             return false;
  70.         }
  71.         BitArray64 value = obj as BitArray64;
  72.         return this.bits == value.bits;      
  73.     }
  74.     //enumerator
  75.     IEnumerator IEnumerable.GetEnumerator()
  76.     {
  77.         return this.GetEnumerator();
  78.     }
  79.  
  80.     public IEnumerator<int> GetEnumerator()
  81.     {
  82.         for (int i = Max; i >= 0; i--)
  83.         {
  84.             yield return this[i];
  85.         }
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment