Advertisement
Josif_tepe

Untitled

Feb 28th, 2022
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. void print_binary(int x) {
  6.     string s = "";
  7.     int max_power = log2(x);
  8.     cout << max_power << endl;
  9.     for(int i = 0; i <= max_power; i++) {
  10.         if(x & (1 << i)) {
  11.             s += "1";
  12.         }
  13.         else {
  14.             s += "0";
  15.         }
  16.        
  17.     }
  18.     reverse(s.begin(), s.end());
  19.     cout << s << endl;
  20. }
  21. int main() {
  22.    
  23.     print_binary(5237);
  24.     return 0;
  25. }
  26.  
  27. // 1010001110101
  28. // 1010001110101
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement