jasonpogi1669

Find the Power of Two that is Smaller or Equal to n

May 26th, 2021 (edited)
157
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. int main() {
  6.     int n;
  7.     cin >> n;
  8.     int exponent = 0;
  9.     for (int i = 0; i < 30; i++) {
  10.         // find the highest power of two that can still divide 'n'
  11.         if ((n >> i) == 1) {
  12.             exponent = i;
  13.         }
  14.     }
  15.     cout << (1 << exponent) << '\n';
  16.     return 0;
  17. }
  18.  
Add Comment
Please, Sign In to add comment