fork download
  1. #include <iostream>
  2. #include <limits>
  3. #include <bitset>
  4. #include <type_traits>
  5. using namespace std;
  6.  
  7. template <typename T>
  8. void printBitRepresentation(T x) {
  9. using unsignedT = typename std::make_unsigned<T>::type;
  10. auto value = static_cast<unsignedT>(x);
  11. cout << bitset<numeric_limits<unsignedT>::digits>(value) << endl;
  12. }
  13.  
  14. template <typename T>
  15. T getNextHigherNumberContainingEqualSetBits(T x) {
  16. if (x == 0) return 0;
  17. T a = x & -x;
  18. T b = a + x;
  19. T r = b | (((x ^ b) >> 2) / a);
  20. return r;
  21. }
  22. template <typename T>
  23. void generateAllSubsets(T start) {
  24. printBitRepresentation(start);
  25. for (auto cur = getNextHigherNumberContainingEqualSetBits(start); cur > start; cur = getNextHigherNumberContainingEqualSetBits(cur))
  26. printBitRepresentation(cur);
  27. }
  28.  
  29. int main() {
  30. const auto bitsize = numeric_limits<unsigned int>::digits;
  31. generateAllSubsets((1u << (bitsize-1)) - 1);
  32. return 0;
  33. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
01111111111111111111111111111111
10111111111111111111111111111111
11011111111111111111111111111111
11101111111111111111111111111111
11110111111111111111111111111111
11111011111111111111111111111111
11111101111111111111111111111111
11111110111111111111111111111111
11111111011111111111111111111111
11111111101111111111111111111111
11111111110111111111111111111111
11111111111011111111111111111111
11111111111101111111111111111111
11111111111110111111111111111111
11111111111111011111111111111111
11111111111111101111111111111111
11111111111111110111111111111111
11111111111111111011111111111111
11111111111111111101111111111111
11111111111111111110111111111111
11111111111111111111011111111111
11111111111111111111101111111111
11111111111111111111110111111111
11111111111111111111111011111111
11111111111111111111111101111111
11111111111111111111111110111111
11111111111111111111111111011111
11111111111111111111111111101111
11111111111111111111111111110111
11111111111111111111111111111011
11111111111111111111111111111101
11111111111111111111111111111110