Given a positive number n, find the previous power of 2. If n itself is a power of 2, return n.

For example,

Input:  n = 20
Output: 16
 
Input:  n = 16
Output: 16

Practice this problem

Approach 1

The idea is to unset the rightmost bit of n until only one bit is left, which will be the last set bit of the given number and a previous power of 2. This approach is demonstrated below in C++, Java, and Python:

C++


Download  Run Code

Output:

The previous power of 2 is 64

Java


Download  Run Code

Output:

The previous power of 2 is 64

Python


Download  Run Code

Output:

The previous power of 2 is 64

Approach 2

The idea is to run a loop by initializing the result by 1. We double the result value at each iteration of the loop and divide n in half and continue the loop till n becomes 0.

Following is the implementation in C++, Java, and Python based on the above idea:

C++


Download  Run Code

Output:

The previous power of 2 is 64

Java


Download  Run Code

Output:

The previous power of 2 is 64

Python


Download  Run Code

Output:

The previous power of 2 is 64

Approach 3

The idea is to calculate the position p of the last set bit of n and return a number with its p'th bit set. In other words, drop all set bits from n except its last set bit.

The implementation can be seen below in C++, Java, and Python:

C++


Download  Run Code

Output:

The previous power of 2 is 16

Java


Download  Run Code

Output:

The previous power of 2 is 16

Python


Download  Run Code

Output:

The previous power of 2 is 16

Approach 4

The idea is to set all bits on the right-hand side of the most significant set bit to 1 and then drop all but the last set bit from n so that it becomes equal to the previous power of two. For instance, consider number 20. We convert its binary representation 00010100 to 00011111. Then drop all set bits except the last one to become 00010000, which is equal to 16 (the previous power of 20).

Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

The previous power of 2 is 128

Java


Download  Run Code

Output:

The previous power of 2 is 128

Python


Download  Run Code

Output:

The previous power of 2 is 128

How does Right shift and bitwise OR operation works?

Let’s take an example of 130, which is 10000010 in binary:

n |= n >> 1;    // 10000010 | 01000001 = 11000011
n |= n >> 2;    // 11000011 | 00110000 = 11110011
n |= n >> 4;    // 11110011 | 00001111 = 11111111
n |= n >> 8;    // … (At this point, all bits are 1, so further bitwise OR
n |= n >> 16;   // operations produce no effect)
n – (n >> 1);   // 11111111 – 01111111 ——> 10000000

There’s one bit in the 8th position, which represents 27, which is indeed the previous largest power of 2. Each of the shifts overlaps all the existing 1 bits in the number with some previously untouched 0’s, eventually producing the total number of 1 bit equal to the total number of bits in the original number. We finally drop all bits except the last set bit.

 
There are several other solutions possible for this problem.

 
References: Hacker’s Delight