Smallest perfect power of 2 greater than n (without using arithmetic operators)
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++
// 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 n unsigned 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 above int main() { unsigned int n = 128; cout << "Perfect power of 2 greater than " << n << ": " << perfectPowerOf2(n); return 0; } |
chevron_right
filter_none
Java
// JAVA Code for Smallest perfect // power of 2 greater than n import 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. |
chevron_right
filter_none
Python3
# Python3 implementation of smallest # perfect power of 2 greater than n # Function to find smallest perfect # power of 2 greater than n def 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 above n = 128print("Perfect power of 2 greater than", n, ":",perfectPowerOf2(n)) # This code is contributed by "Sharad_Bhardwaj". |
chevron_right
filter_none
C#
// C# Code for Smallest perfect // power of 2 greater than n using 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 |
chevron_right
filter_none
PHP
<?php // php implementation of // smallest perfect power // of 2 greater than n // Function to find smallest // perfect power of 2 // greater than n function 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 ?> |
chevron_right
filter_none
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.
Recommended Posts:
- Program to find whether a no is power of two
- Smallest power of 2 greater than or equal to n
- Compute modulus division by a power-of-2-number
- Find whether a given number is a power of 4 or not
- Smallest of three integers without comparison operators
- Add two numbers without using arithmetic operators
- Bitwise right shift operators in Java
- Russian Peasant (Multiply two numbers using bitwise operators)
- Bitwise Operators in C/C++
- Check if a number is multiple of 9 using bitwise operators
- Subtract two numbers without using arithmetic operators
- Calculate 7n/8 without using division and multiplication operators
- Highest power of 2 less than or equal to given number
- Count smaller numbers whose XOR with n produces greater value
- Count smaller values whose XOR with x is greater than x
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar



