Advertisement
phi2dao

Bitset

Sep 27th, 2024
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. public struct Bitset(int data = 0) : IEquatable<Bitset>
  2. {
  3. public bool this[int i]
  4. {
  5. get
  6. {
  7. if (i < 0 or >= 32)
  8. throw new ArgumentOutOfRangeException();
  9. int mask = 1 << i;
  10. return (_data & mask) == mask;
  11. }
  12. set
  13. {
  14. if (i < 0 or >= 32)
  15. throw new ArgumentOutOfRangeException();
  16. int mask = 1 << i;
  17. value ? _data |= mask : _data &= ~mask;
  18. }
  19. }
  20.  
  21. public bool Equals(Bitset other) => _data == other._data;
  22. public override bool Equals(object? o) => o is Bitset other && Equals(other);
  23. public override int GetHashCode() => _data.GetHashCode();
  24.  
  25. public static operator ==(Bitset left, Bitset right) => left.Equals(right);
  26. public static operator !=(Bitset left, Bitset right) => !left.Equals(right);
  27.  
  28. private int _data = data;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement