fork(3) download
  1. #include <bitset>
  2. #include <sstream>
  3. #include <iostream>
  4. #include <cinttypes>
  5.  
  6. namespace my
  7. {
  8. template<size_t S>
  9. std::istream &operator >> (std::istream &is, std::bitset<S> &bits)
  10. {
  11. std::uint8_t byte;
  12. size_t i = 0;
  13. while(i < S && (is >> byte))
  14. for(size_t j = 0; j < 8 && i < S; ++j)
  15. bits[i++] = (byte >> j) & 1;
  16. return is;
  17. }
  18. }
  19.  
  20. int main()
  21. {
  22.  
  23. constexpr size_t bytes = 2;
  24. std::string bit_string("\x00\xFF", bytes);
  25. std::istringstream bit_stream(bit_string);
  26.  
  27. std::bitset<8 * bytes> b;
  28.  
  29. {
  30. using namespace my;
  31. bit_stream >> b;
  32. }
  33.  
  34. std::cout << b << std::endl;
  35.  
  36. for(size_t i = 0; i < b.size(); ++i)
  37. std::cout << b[i];
  38.  
  39. std::cout << std::endl;
  40. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
1111111100000000
0000000011111111