Advertisement
aaronhma

Get k-th Bit

May 18th, 2022
692
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. int main()
  6. {
  7.   int n = 4, k = 2; // k is 0-based indexing!
  8.  
  9.   cout << n << "\n"; // 4 (100)
  10.   cout << (n >> k) << "\n"; // 1 (1)
  11.   cout << ((n >> k) & 1) << "\n"; // 1
  12.  
  13.   return 0;
  14. }
Advertisement
RAW Paste Data Copied
Advertisement