text: Smallest power of 2 greater than or equal to n | C ++ program to Finds next power of two for n . If n itself is a power of two then returns n ; Finds next power of two for n . If n itself is a power of two then returns n ; Driver Code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int nextPowerOf2 ( unsigned int n ) { n -- ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n ++ ; return n ; } int main ( ) { unsigned int n = 5 ; cout << nextPowerOf2 ( n ) ; return 0 ; }