Advertisement
Infiniti_Inter

Binary to hex

Mar 15th, 2019
181
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 <math.h>
  3. using namespace std;
  4. template <typename X>
  5. inline X abs(const X &a) { return a < 0 ? -a : a; }
  6.  
  7. int main()
  8. {
  9.     int a; cin >> a;
  10.     cout << "abs: " << abs(a) << endl;
  11.     cout << "int: " << a << endl;
  12.     cout << "binary abs: ";
  13.     for(int i = 7;i>=0;i--)
  14.         cout<< bool(abs(a)&(1<<i));
  15.     cout << endl;
  16.     cout << "binary: ";
  17.     for(int i = 7;i>=0;i--)
  18.         cout<< bool(a&(1<<i));
  19.     cout << endl;
  20.     cout.setf(ios::showbase);
  21.     cout << "hex: " << hex << int(char(a));
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement