Smallest perfect power of 2 greater than n ( without using arithmetic operators ) | C ++ implementation of smallest perfect power of 2 greater than n ; Function to find smallest perfect power of 2 greater than n ; To store perfect power of 2 ; bitwise left shift by 1 ; bitwise right shift by 1 ; Required perfect power of 2 ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int perfectPowerOf2 ( unsigned int n ) { unsigned int per_pow = 1 ; while ( n > 0 ) { per_pow = per_pow << 1 ; n = n >> 1 ; } return per_pow ; } int main ( ) { unsigned int n = 128 ; cout << " Perfect ▁ power ▁ of ▁ 2 ▁ greater ▁ than ▁ " << n << " : ▁ " << perfectPowerOf2 ( n ) ; return 0 ; }