Advertisement
aayyk

Untitled

Oct 26th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. template <size_t N>
  2. struct Bitset {
  3.  private: unsigned long long a[(N + 63) / 64];
  4.  
  5.     Bitset() {
  6.         memset(a, 0, sizeof a);
  7.     }
  8.  
  9.     friend Bitset<N> operator^(const Bitset<N>& a, const Bitset<N>& b);
  10.  
  11.     inline bool get(size_t i) const {
  12.         return a[i / 64] >> (i % 64) & 1;
  13.     }
  14.  
  15.     inline void set(size_t i, bool val) {
  16.         a[i / 64] &= ~0ULL ^ (1ULL << (i % 64));
  17.         a[i / 64] |= static_cast<unsigned long long>(val) << (i % 64);
  18.     }
  19.  
  20.     size_t ffs() {
  21.         for (size_t i = 0; i < (N + 63) / 64; i++) {
  22.             if (a[i]) {
  23.                 for (size_t j = 0; ; j++) {
  24.                     if (a[i] >> j & 1) {
  25.                         return i * 64 + j;
  26.                     }
  27.                 }
  28.             }
  29.         }
  30.         return N;
  31.     }
  32.  
  33.     operator string() {
  34.         string res;
  35.         for (size_t i = 0; i < N; i++) {
  36.             res += '0' + get(i);
  37.         }
  38.         return res;
  39.     }
  40. };
  41.  
  42. template <size_t N>
  43. Bitset<N> operator^(const Bitset<N>& a, const Bitset<N>& b) {
  44.     Bitset<N> c;
  45.     for (size_t i = 0; i < (N + 63) / 64; i++) {
  46.         c.a[i] = a.a[i] ^ b.a[i];
  47.     }
  48.     return c;
  49. }
  50.  
  51. template <size_t N>
  52. bool operator<(const Bitset<N>& a, const Bitset<N>& b) {
  53.     size_t i = (a ^ b).ffs();
  54.     return (i < N ? !a.get(i) : false);
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement