fork(3) download
  1. #include <iterator>
  2. #include <climits>
  3. #include <iostream>
  4.  
  5. //this version of gcc doesn't seem to have begin/end so I add them here
  6. namespace std {
  7. template< class T, size_t N >
  8. T* begin( T (&array)[N] ) {return array;}
  9. template< class T, size_t N >
  10. T* end( T (&array)[N] ) {return array+N;}
  11. }
  12. //you shouldn't have to do this
  13.  
  14. template<class output_iterator>
  15. void convert_number_to_array_of_digits(const unsigned number,
  16. output_iterator first, output_iterator last)
  17. {
  18. const unsigned number_bits = CHAR_BIT*sizeof(int);
  19. //extract bits one at a time
  20. for(unsigned i=0; i<number_bits && first!=last; ++i) {
  21. const unsigned shift_amount = number_bits-i-1;
  22. const unsigned this_bit = (number>>shift_amount)&1;
  23. *first = this_bit;
  24. ++first;
  25. }
  26. //pad the rest with zeros
  27. while(first != last) {
  28. *first = 0;
  29. ++first;
  30. }
  31. }
  32.  
  33. int main() {
  34. int number = 413523152;
  35. int array[32];
  36. convert_number_to_array_of_digits(number, std::begin(array), std::end(array));
  37. for(int i=0; i<32; ++i)
  38. std::cout << array[i] << ' ';
  39. }
Success #stdin #stdout 0s 2928KB
stdin
Standard input is empty
stdout
0 0 0 1 1 0 0 0 1 0 1 0 0 1 0 1 1 1 0 1 1 1 0 0 1 1 0 1 0 0 0 0