fork download
  1. #include <iostream>
  2. #include <bitset>
  3.  
  4. template <size_t N>
  5. class twoBit
  6. {
  7. typedef typename std::bitset<2*N>::reference bitRef;
  8. bitRef a, b;
  9. public:
  10. twoBit(bitRef a1, bitRef b1): a(a1), b(b1) {};
  11. const twoBit &operator=(int i) { a = i%2; b = i/2; return *this; };
  12. operator int() { return 2*b + a; };
  13. };
  14.  
  15. template <size_t N>
  16. class twoBitSet : private std::bitset<2*N>
  17. {
  18. typedef typename std::bitset<2*N>::reference bitRef;
  19. public:
  20. twoBit<N> operator[](int index)
  21. {
  22. bitRef b1 = std::bitset<2*N>::operator[](2*index);
  23. bitRef b2 = std::bitset<2*N>::operator[](2*index + 1);
  24. return twoBit<N>(b1, b2);
  25. };
  26. };
  27.  
  28. int main()
  29. {
  30. twoBitSet<32> bs;
  31. bs[0] = 2;
  32. bs[1] = 3;
  33. bs[2] = 1;
  34. bs[3] = 0;
  35. std::cout << bs[0] << std::endl; // prints 2
  36. std::cout << bs[1] << std::endl; // prints 3
  37. std::cout << bs[2] << std::endl; // prints 1
  38. std::cout << bs[3] << std::endl; // prints 0
  39. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
2
3
1
0