Smallest perfect power of 2 greater than n (without using arithmetic operators) - GeeksforGeeks

05 Mar, 2021
Given a non-negative integer n. The problem is to find the smallest perfect power of 2 which is greater than n without using the arithmetic operators.Examples :  
Input : n = 10
Output : 16

Input : n = 128
Output : 256
 
Algorithm :  
 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of smallest perfect power// of 2 greater than n#include <bits/stdc++.h> using namespace std; // Function to find smallest perfect power// of 2 greater than nunsigned int perfectPowerOf2(unsigned int n){    // To store perfect power of 2    unsigned int per_pow = 1;     while (n > 0)    {        // bitwise left shift by 1        per_pow = per_pow << 1;         // bitwise right shift by 1        n = n >> 1;    }     // Required perfect power of 2    return per_pow;} // Driver program to test aboveint main(){    unsigned int n = 128;    cout << "Perfect power of 2 greater than "        << n << ": " << perfectPowerOf2(n);    return 0;}
// JAVA Code for Smallest perfect// power of 2 greater than nimport java.util.*; class GFG {         // Function to find smallest perfect    // power of 2 greater than n    static int perfectPowerOf2( int n)    {        // To store perfect power of 2         int per_pow = 1;              while (n > 0)        {            // bitwise left shift by 1            per_pow = per_pow << 1;                         n = n >> 1;        }              // Required perfect power of 2        return per_pow;    }         // Driver program    public static void main(String[] args)    {         int n = 12;         System.out.println("Perfect power of 2 greater than "                            + n + ": " + perfectPowerOf2(n));        }    }             //This code is contributed by Arnav Kr. Mandal.
# Python3 implementation of smallest# perfect power of 2 greater than n # Function to find smallest perfect# power of 2 greater than ndef perfectPowerOf2( n ):         # To store perfect power of 2    per_pow = 1         while n > 0:             # bitwise left shift by 1        per_pow = per_pow << 1                 # bitwise right shift by 1        n = n >> 1             # Required perfect power of 2    return per_pow # Driver program to test aboven = 128print("Perfect power of 2 greater than",            n, ":",perfectPowerOf2(n)) # This code is contributed by "Sharad_Bhardwaj".
// C# Code for Smallest perfect// power of 2 greater than nusing System; class GFG {     // Function to find smallest perfect    // power of 2 greater than n    static int perfectPowerOf2(int n)    {        // To store perfect power of 2        int per_pow = 1;         while (n > 0)        {            // bitwise left shift by 1            per_pow = per_pow << 1;             n = n >> 1;        }         // Required perfect power of 2        return per_pow;    }     // Driver program    public static void Main()    {        int n = 128;        Console.WriteLine("Perfect power of 2 greater than " +                           n + ": " + perfectPowerOf2(n));    }} // This code is contributed by Sam007
<?php// php implementation of// smallest perfect power// of 2 greater than n // Function to find smallest// perfect power of 2// greater than nfunction perfectPowerOf2($n){         // To store perfect power of 2    $per_pow = 1;     while ($n > 0)    {        // bitwise left shift by 1        $per_pow = $per_pow << 1;         // bitwise right shift by 1        $n = $n >> 1;    }     // Required perfect power of 2    return $per_pow;}     // Driver code    $n = 128;    echo "Perfect power of 2 greater than ".          $n . ": ".perfectPowerOf2($n); // This code is contributed by mits?>
<script> // JavaScript implementation of smallest perfect power// of 2 greater than n // Function to find smallest perfect power// of 2 greater than nfunction perfectPowerOf2(n){    // To store perfect power of 2    let per_pow = 1;     while (n > 0)    {        // bitwise left shift by 1        per_pow = per_pow << 1;         // bitwise right shift by 1        n = n >> 1;    }     // Required perfect power of 2    return per_pow;} // Driver program to test above     let n = 128;    document.write("Perfect power of 2 greater than "        + n + ": " + perfectPowerOf2(n));  // This code is contributed by Surbhi Tyagi. </script>
Output: 
Perfect power of 2 greater than 128: 256
Time Complexity: O(num), where num is the number of bits in the binary representation of n. 
Mithun Kumar
surbhityagi15
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Find the size of Largest Subset with positive Bitwise AND
Check whether bitwise AND of a number with any subset of an array is zero or not
Write an Efficient Method to Check if a Number is Multiple of 3
Highest power of 2 less than or equal to given number
Swap two nibbles in a byte
Check for Integer Overflow
Swap bits in a given number
Reverse actual bits of the given number
Bit Tricks for Competitive Programming