Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BitArray64 : IEnumerable<int>
- {
- private ulong bits;
- private const int Max = 63;
- public BitArray64(ulong bits)
- {
- this.bits = bits;
- }
- public int this [int index]
- {
- get
- {
- if (index < 0 || index > Max)
- {
- throw new IndexOutOfRangeException("Index must be >= 0 and <= 63");
- }
- if ( (bits & (ulong)1 << index ) == 0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- set
- {
- if (index < 0 || index > Max)
- {
- throw new IndexOutOfRangeException("Index must be >= 0 and <= 63");
- }
- if (value != 0 && value != 1)
- {
- throw new ArgumentException("Value must be 1 or 0");
- }
- if (value == 1)
- {
- this.bits = this.bits | ((ulong)1 << index);
- }
- else
- {
- this.bits = this.bits & (~((ulong)1 << index));
- }
- }
- }
- public override int GetHashCode()
- {
- return this.GetHashCode();
- }
- public static bool operator ==(BitArray64 bit1, BitArray64 bit2)
- {
- return (BitArray64.Equals(bit1, bit2));
- }
- public static bool operator !=(BitArray64 bit1, BitArray64 bit2)
- {
- return !(BitArray64.Equals(bit1, bit2));
- }
- public override bool Equals(object obj)
- {
- if (obj == null)
- {
- return false;
- }
- BitArray64 value = obj as BitArray64;
- return this.bits == value.bits;
- }
- //enumerator
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- public IEnumerator<int> GetEnumerator()
- {
- for (int i = Max; i >= 0; i--)
- {
- yield return this[i];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment