Advertisement
aaronhma

Print a Binary Number with a Single 1 Bit in the n-th position

May 18th, 2022
703
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define N 64
  6.  
  7. int main()
  8. {
  9.   int n = 4;
  10.   int ans = (1 << (n - 1));
  11.   string binary = bitset<N>(ans).to_string();
  12.  
  13.   cout << binary << "\n"; // 1000
  14.  
  15.   return 0;
  16. }
Advertisement
RAW Paste Data Copied
Advertisement