fork download
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using std::endl;
  5. using std::cout;
  6. using std::string;
  7. using std::ostringstream;
  8.  
  9. template <unsigned int N>
  10. struct byte_t {
  11. bool bits[N];
  12. byte_t() {
  13. for (int n = N; n > 0; n--)
  14. bits[n] = false;
  15. cout << "Created " << N << "-bit byte: " << str() << endl;
  16. }
  17. string dstr() {
  18. ostringstream ss;
  19. for (int n = N; n > 0; n--)
  20. if (bits[n] == true) ss << '1';
  21. else ss << '0';
  22. return ss.str();
  23. }
  24. string str() {
  25. ostringstream ss;
  26. for (int n = N; n > 0; n--)
  27. ss << bits[n];
  28. return ss.str();
  29. }
  30. };
  31.  
  32. int main(int argc,char** argv) {
  33. byte_t<8> my_byte;
  34. cout << my_byte.str() << endl;
  35. cout << my_byte.str() << endl;
  36. cout << my_byte.dstr() << endl;
  37. return 0;
  38. }
  39.  
  40.  
Success #stdin #stdout 0s 3036KB
stdin
Standard input is empty
stdout
Created 8-bit byte: 00000000
00000000
00000000
00000000