Advertisement
pneave

bitset

Nov 22nd, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <bitset>
  3.  
  4. // The Object that we put on the stream.
  5. // Pass in the group length, or default to 4.
  6. class bitpr
  7. {
  8.   public:
  9.     bitpr(size_t l = 4)
  10.       :m_l(l){}
  11.  
  12.   private:
  13.     // Class that actually does the work.
  14.     class bitprn
  15.     {
  16.       public:
  17.         bitprn(bitpr const& b, std::ostream& os)
  18.           : m_l(b.m_l),
  19.             m_s(os){}
  20.  
  21.         // The << operator for bitset<> types. Outputs the formatted
  22.         // object to the stored stream then returns the stream.
  23.         template<size_t N>
  24.         std::ostream& operator<<(std::bitset<N> const& b)
  25.         {
  26.           int bit_len = b.size();
  27.  
  28.           for (int i = bit_len - 1; i >= 0; --i)
  29.           {
  30.             m_s << b[i];
  31.             if ((i == (bit_len - bit_len % m_l)) ||
  32.                 (0 == i % m_l)) m_s << ' ';
  33.           }
  34.           return m_s << '\b';
  35.         }
  36.  
  37.         // The << operator for all other types. Outputs the next object
  38.         // to the stored stream then returns the stream.
  39.         template<typename T>
  40.         std::ostream& operator<<(T const& o)
  41.         {
  42.           return m_s << o;
  43.         }
  44.  
  45.       private:
  46.         size_t        m_l;
  47.         std::ostream& m_s;
  48.     };
  49.     friend bitpr::bitprn operator<<(std::ostream& os, bitpr const& b);
  50.  
  51.   private:
  52.     char m_l;
  53. };
  54.  
  55. // When you pass an object of type bitpr to an ostream it returns
  56. // an object of bitpr::bitprn that has overloaded the << operator for
  57. // all types. This will process the next object and then return the stream
  58. // to continue processing as normal.
  59. bitpr::bitprn operator<<(std::ostream& os, bitpr const& b)
  60. {
  61.   return bitpr::bitprn(b, os);
  62. }
  63.  
  64. using namespace std;
  65.  
  66. int main(int, char**)
  67. {
  68.   bitset<1> a;
  69.   bitset<2> b;
  70.   bitset<3> c;
  71.   bitset<4> d;
  72.   bitset<5> e;
  73.   bitset<6> f;
  74.   bitset<11> g;
  75.   bitset<12> h;
  76.   bitset<16> i;
  77.   bitset<23> j;
  78.  
  79.   cout << "a: " << a << endl;
  80.   cout << "b: " << b << endl;
  81.   cout << "c: " << c << endl;
  82.   cout << "d: " << d << endl;
  83.   cout << "e: " << e << endl;
  84.   cout << "f: " << f << endl;
  85.   cout << "g: " << g << endl;
  86.   cout << "h: " << h << endl;
  87.   cout << "i: " << i << endl;
  88.   cout << "j: " << j << endl;
  89.  
  90.   cout << "---------------------------------------\n";
  91.  
  92.   cout << "a: " << bitpr() << a << endl;
  93.   cout << "b: " << bitpr() << b << endl;
  94.   cout << "c: " << bitpr() << c << endl;
  95.   cout << "d: " << bitpr() << d << endl;
  96.   cout << "e: " << bitpr() << e << endl;
  97.   cout << "f: " << bitpr() << f << endl;
  98.   cout << "g: " << bitpr() << g << endl;
  99.   cout << "h: " << bitpr() << h << endl;
  100.   cout << "i: " << bitpr() << i << endl;
  101.   cout << "j: " << bitpr(8) << j << endl;
  102.  
  103.   // Any other type is unchanged
  104.   cout << "This is a string: " << bitpr() << "string" << endl;
  105.   cout << "This is an int  : " << bitpr() << 123 << endl;
  106.   cout << "This is a float : " << bitpr() << 3.14159 << endl;
  107.  
  108.   return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement