fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template<long long X>
  5. class Bitset
  6. {
  7. private:
  8. std::vector<unsigned char> bits = std::vector<unsigned char> ((X+7)/8);
  9.  
  10. public:
  11. /* constructors */
  12.  
  13. class BitProxy
  14. {
  15. private:
  16. unsigned char &bit;
  17.  
  18. public:
  19. BitProxy(unsigned char &bit) : bit(bit) {}
  20.  
  21. BitProxy& operator=(unsigned char x) { bit = x; return *this; }
  22. operator unsigned char() const { return bit; }
  23. };
  24.  
  25. BitProxy operator[](size_t index) { return BitProxy(bits[index]); }
  26.  
  27. friend std::ostream& operator<< (std::ostream &output, const BitProxy &x)
  28. {
  29. output << static_cast<short>(static_cast<unsigned char>(x));
  30. return output;
  31. }
  32. };
  33.  
  34. int main()
  35. {
  36. Bitset<1> a;
  37. a[0] = 0xff;
  38. std::cout << a[0] << std::endl;
  39. std::cout << 'a' << std::endl;
  40. return 0;
  41. }
Success #stdin #stdout 0s 4460KB
stdin
Standard input is empty
stdout
255
a