fork(2) download
  1. #include <iostream>
  2. #include <bitset>
  3. #include <climits>
  4. void print_bin(int num)
  5. {
  6. std::string bin = std::bitset<CHAR_BIT * sizeof num>(num).to_string();
  7. // trim leading zeroes
  8. bin = num ? bin.substr(bin.find('1')) : "0";
  9. std::cout << num << ": [0B" << bin << ']' << '\n';
  10. }
  11. int main()
  12. {
  13. print_bin(5);
  14. print_bin(42);
  15. }
  16.  
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
5: [0B101]
42: [0B101010]