fork download
  1. //Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86
  2.  
  3. // convert_to_bits()
  4. #include <bitset>
  5. #include <type_traits>
  6. #include <algorithm>
  7. #include <climits>
  8.  
  9. // main()
  10. #include <iostream>
  11. #include <sstream>
  12.  
  13. template<typename FromT, size_t FromSZ, typename ToT, size_t ToSZ>
  14. void convert_to_bits(const FromT (&from_arr)[FromSZ], ToT (&to_arr)[ToSZ]) {
  15. static_assert(ToSZ >= ((sizeof(FromT) * CHAR_BIT) * FromSZ),
  16. "Destination array must have enough elements to contain all bits of source array.");
  17.  
  18. // Helper value, will be optimised out.
  19. constexpr size_t ELEMENT_SIZE = sizeof(FromT) * CHAR_BIT;
  20.  
  21. // Handles bit manipulation cleanly.
  22. std::bitset<ELEMENT_SIZE> temp[FromSZ];
  23.  
  24. // Populate temp.
  25. for (size_t i = 0; i < FromSZ; i++) { temp[i] = from_arr[i]; }
  26.  
  27. // Fill destination array, one "row" (value in source array) at a time.
  28. for (size_t i = 0; i < FromSZ; i++) {
  29. size_t row = i * ELEMENT_SIZE;
  30. using std::fill;
  31.  
  32. if (temp[i].none()) {
  33. // Row is all 0s, fill its part of destination array appropriately.
  34. fill(&to_arr[row], &to_arr[row + (ELEMENT_SIZE - 1)], 0);
  35. } else if (temp[i].all()) {
  36. // Row is all 1s, fill its part of destination array appropriately.
  37. fill(&to_arr[row], &to_arr[row + (ELEMENT_SIZE - 1)], 1);
  38. } else {
  39. // Row is mixed, do it one bit at a time.
  40. for (size_t j = 0; j < ELEMENT_SIZE; j++) {
  41. to_arr[row + j] = temp[i][j];
  42. }
  43. }
  44. }
  45. }
  46.  
  47. template<typename T, size_t N>
  48. constexpr size_t arr_size(T (&arr)[N]) { return N; }
  49.  
  50. struct Results {
  51. std::stringstream base;
  52. std::stringstream answ;
  53.  
  54. bool compare() { return base.str() == answ.str(); }
  55. };
  56.  
  57. int main() {
  58. // Data:
  59. constexpr size_t INT_SZ = sizeof(int) * CHAR_BIT;
  60.  
  61. int Data[8] = {0,0,190,42,0,0,2,33};
  62. int bits[8 * INT_SZ] = {0};
  63.  
  64. // -----
  65.  
  66. // Work:
  67. convert_to_bits(Data, bits);
  68.  
  69. // -----
  70.  
  71. // Obtain results, for testing.
  72. Results results;
  73.  
  74. // Original data.
  75. for (size_t i = 0; i < arr_size(Data); i++) {
  76. for (size_t j = 0; j < INT_SZ; j++) {
  77. results.base << ((Data[i] & (1 << j)) ? 1 : 0);
  78. }
  79. results.base << '\n';
  80. }
  81. results.base << '\n';
  82.  
  83. // Converted data.
  84. for (size_t i = 0; i < arr_size(bits); i++) {
  85. results.answ << bits[i]
  86. // Mimics results.base's outer loop.
  87. << ((i + 1) % INT_SZ == 0 ? "\n" : "");
  88. }
  89. results.answ << '\n';
  90.  
  91. // -----
  92.  
  93. // Output and compare.
  94. std::cout << "Original data:\n" << results.base.str();
  95. std::cout << "Results:\n" << results.answ.str();
  96. std::cout << "Results are "
  97. << (results.compare() ? "identical" : "incorrect")
  98. << ".\n";
  99. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Original data:
00000000000000000000000000000000
00000000000000000000000000000000
01111101000000000000000000000000
01010100000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
01000000000000000000000000000000
10000100000000000000000000000000

Results:
00000000000000000000000000000000
00000000000000000000000000000000
01111101000000000000000000000000
01010100000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
01000000000000000000000000000000
10000100000000000000000000000000

Results are identical.