Next Power of Two

Last Updated : 8 Jul, 2026

Given a positive integer n, find the smallest power of 2 that is greater than or equal to n. If n is already a power of 2, return n.

Examples : 

Input: n = 5
Output: 8     
Explanation: The powers of 2 around 5 are 4 and 8. Since 8 is the smallest power of 2 greater than or equal to 5, the answer is 8.

Input: n = 16
Output: 16 
Explanation: 16 is already a power of 2. Hence, the answer is 16.

Try It Yourself
redirect icon

[Naive Approach] Generate Powers of 2 Until Reaching n - O(log n) Time and O(1) Space

The simplest idea is to start from the first power of 2, i.e., 1, and keep multiplying by 2 until the value becomes greater than or equal to n. Since every multiplication generates the next power of 2, the first power that becomes at least n is the required answer.

C++
#include <iostream>
using namespace std;

// Return the smallest power of 2 greater than or equal to n.
int nextPowerOfTwo(int n) {

    // Start from the first power of 2.
    int power = 1;

    // Generate powers of 2 until power becomes at least n.
    while (power < n)
        power *= 2;

    return power;
}

int main() {
    int n = 5;
    cout << nextPowerOfTwo(n);
    return 0;
}
Java
public class Main {

    // Return the smallest power of 2 greater than or equal to n.
    static int nextPowerOfTwo(int n) {

        // Start from the first power of 2.
        int power = 1;

        // Generate powers of 2 until power becomes at least n.
        while (power < n)
            power *= 2;

        return power;
    }

    public static void main(String[] args) {
        int n = 5;
        System.out.println(nextPowerOfTwo(n));
    }
}
Python
# Return the smallest power of 2 greater than or equal to n.
def nextPowerOfTwo(n):

    # Start from the first power of 2.
    power = 1

    # Generate powers of 2 until power becomes at least n.
    while power < n:
        power *= 2

    return power


n = 5
print(nextPowerOfTwo(n))
C#
using System;

class Program
{
    // Return the smallest power of 2 greater than or equal to n.
    static int NextPowerOfTwo(int n)
    {
        // Start from the first power of 2.
        int power = 1;

        // Generate powers of 2 until power becomes at least n.
        while (power < n)
            power *= 2;

        return power;
    }

    static void Main()
    {
        int n = 5;
        Console.WriteLine(NextPowerOfTwo(n));
    }
}
JavaScript
// Return the smallest power of 2 greater than or equal to n.
function nextPowerOfTwo(n) {

    // Start from the first power of 2.
    let power = 1;

    // Generate powers of 2 until power becomes at least n.
    while (power < n)
        power *= 2;

    return power;
}

let n = 5;
console.log(nextPowerOfTwo(n));

Output
8

[Better Approach] Count Bits of (n - 1) - O(log n) Time and O(1) Space

The idea of this approach is to use the number of bits required to represent n - 1. If n is already a power of 2, then n - 1 contains all lower bits set. Otherwise, the number of bits required to represent n - 1 determines the next power of 2. We first decrement n by 1. Then we repeatedly divide it by 2 and count how many times this can be done before it becomes 0.
This count represents the number of bits required to represent n - 1. Finally, the answer is obtained using: 1 << bits. which gives the smallest power of 2 greater than or equal to the original value of n.

  • Decrement n by 1.
  • Count the number of bits required to represent n - 1.
  • Return 1 << bits.
C++
#include <iostream>
using namespace std;

// Return the smallest power of 2 greater than or equal to n.
int nextPowerOfTwo(int n) {

    // Count bits needed to represent (n - 1).
    int bits = 0;

    n--;

    while (n) {
        n /= 2;
        bits++;
    }

    // Return the required power of 2.
    return (1 << bits);
}

int main() {
    int n = 5;
    cout << nextPowerOfTwo(n);
    return 0;
}
Java
public class Main {

    // Return the smallest power of 2 greater than or equal to n.
    static int nextPowerOfTwo(int n) {

        // Count bits needed to represent (n - 1).
        int bits = 0;

        n--;

        while (n > 0) {
            n /= 2;
            bits++;
        }

        // Return the required power of 2.
        return (1 << bits);
    }

    public static void main(String[] args) {
        int n = 5;
        System.out.println(nextPowerOfTwo(n));
    }
}
Python
# Return the smallest power of 2 greater than or equal to n.
def nextPowerOfTwo(n):

    # Count bits needed to represent (n - 1).
    bits = 0

    n -= 1

    while n:
        n //= 2
        bits += 1

    # Return the required power of 2.
    return (1 << bits)


n = 5
print(nextPowerOfTwo(n))
C#
using System;

class Program
{
    // Return the smallest power of 2 greater than or equal to n.
    static int NextPowerOfTwo(int n)
    {
        // Count bits needed to represent (n - 1).
        int bits = 0;

        n--;

        while (n != 0)
        {
            n >>= 1;
            bits++;
        }

        // Return the required power of 2.
        return (1 << bits);
    }

    static void Main()
    {
        int n = 5;
        Console.WriteLine(NextPowerOfTwo(n));
    }
}
JavaScript
// Return the smallest power of 2 greater than or equal to n.
function nextPowerOfTwo(n) {

    // Count bits needed to represent (n - 1).
    let bits = 0;

    n--;

    while (n > 0) {
        n = Math.floor(n / 2);
        bits++;
    }

    // Return the required power of 2.
    return (1 << bits);
}

let n = 5;
console.log(nextPowerOfTwo(n));

Output
8

[Expected Approach] Bit Manipulation Using Bit Propagation - O(1) Time and O(1) Space

The idea of this approach is to first decrement n by 1 and then set all bits after the most significant set bit. This can be done efficiently using a sequence of bitwise OR operations with right-shifted versions of the number. After these operations, all bits from the highest set bit to the least significant bit become 1. Adding 1 then produces the smallest power of 2 that is greater than or equal to the original value of n. This approach performs a fixed number of operations for a 32-bit integer and therefore runs in constant time.

  • Subtract n by 1: n = n -1
  • Set all 32 bits after the leftmost set bit. We mainly do n = n | (n >> 1), n = n | (n >> 2), n = n | (n >> 4), n = n | (n >> 8) and n = n | (n >> 16)
  • Return n + 1

Consider: n = 17

Step 1:
n = n - 1 = 16
Binary = 10000

Step 2:
n = n | (n >> 1)
10000
01000
------
11000
n = n | (n >> 2)
11000
00110
------
11110
n = n | (n >> 4)
11110
00001
------
11111
Further shifts do not change the value.

Step 3:
Return n + 1
11111 + 1 = 100000

Answer = 32

C++
#include <iostream>
using namespace std;

// Return the smallest power of 2 greater than or equal to n.
int nextPowerOfTwo(int n) {

    if (n <= 1)
        return 1;

    // Decrement n by 1.
    n--;

    // Propagate the most significant set bit to all lower positions.
    n |= (n >> 1);
    n |= (n >> 2);
    n |= (n >> 4);
    n |= (n >> 8);
    n |= (n >> 16);

    // Return the next power of 2.
    return n + 1;
}

int main() {
    int n = 17;
    cout << nextPowerOfTwo(n);
    return 0;
}
Java
class GFG {

    // Return the smallest power of 2 greater than or equal to n.
    static int nextPowerOfTwo(int n) {

        if (n <= 1)
            return 1;

        // Decrement n by 1.
        n--;

        // Propagate the most significant set bit to all lower positions.
        n |= (n >> 1);
        n |= (n >> 2);
        n |= (n >> 4);
        n |= (n >> 8);
        n |= (n >> 16);

        // Return the next power of 2.
        return n + 1;
    }

    public static void main(String[] args) {
        int n = 17;
        System.out.print(nextPowerOfTwo(n));
    }
}
Python
# Return the smallest power of 2 greater than or equal to n.
def nextPowerOfTwo(n):

    if n <= 1:
        return 1

    # Decrement n by 1.
    n -= 1

    # Propagate the most significant set bit to all lower positions.
    n |= (n >> 1)
    n |= (n >> 2)
    n |= (n >> 4)
    n |= (n >> 8)
    n |= (n >> 16)

    # Return the next power of 2.
    return n + 1


n = 17
print(nextPowerOfTwo(n))
C#
using System;

class GFG {

    // Return the smallest power of 2 greater than or equal to n.
    static int nextPowerOfTwo(int n) {

        if (n <= 1)
            return 1;

        // Decrement n by 1.
        n--;

        // Propagate the most significant set bit to all lower positions.
        n |= (n >> 1);
        n |= (n >> 2);
        n |= (n >> 4);
        n |= (n >> 8);
        n |= (n >> 16);

        // Return the next power of 2.
        return n + 1;
    }

    static void Main() {
        int n = 17;
        Console.Write(nextPowerOfTwo(n));
    }
}
JavaScript
// Return the smallest power of 2 greater than or equal to n.
function nextPowerOfTwo(n) {

    if (n <= 1)
        return 1;

    // Decrement n by 1.
    n--;

    // Propagate the most significant set bit to all lower positions.
    n |= (n >> 1);
    n |= (n >> 2);
    n |= (n >> 4);
    n |= (n >> 8);
    n |= (n >> 16);

    // Return the next power of 2.
    return n + 1;
}

let n = 17;
console.log(nextPowerOfTwo(n));

Output
32
Comment