aaronhma

Extract the Rightmost 1-bit

May 18th, 2022 (edited)
748
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // other version here: https://pastebin.com/phvkXRVN
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. void print(int f)
  7. {
  8.   int ans = 0;
  9.  
  10.   while (f) // break when f = 0
  11.   {
  12.     // Get value at least significant bit position
  13.     if (f & 1)
  14.       break;
  15.     ans++;
  16.     f >>= 1; // remove least significant bit position
  17.   }
  18.  
  19.   cout << (1 << ans) << "\n";
  20. }
  21.  
  22. int main()
  23. {
  24.   print(0b110100);
  25.  
  26.   return 0;
  27. }
RAW Paste Data Copied