Advertisement
jasonpogi1669

Convert Decimal to Binary using Bitwise Operators in C++

Dec 16th, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void ConvertToBinary(int n) {
  6.     // size of the integer is assumed to be 32-bits
  7.     for (int i = 31; i >= 0; i--) {
  8.         int k = n >> i; // shifts all bits rightwards i times
  9.         cout << (k & 1 ? '1' : '0') << " ";
  10.     }
  11. }
  12.  
  13. int main() {
  14.     int n = 13;
  15.     ConvertToBinary(n);
  16.     cout << '\n';
  17.     return 0;
  18. }
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement