Related Articles

# Highest power of 2 less than or equal to given Integer

• Difficulty Level : Basic
• Last Updated : 15 Apr, 2021

Given an integer N, the task is to find the highest power of 2 that is smaller than or equal to N.

Examples:

Input: N = 9
Output:
Explanation:
Highest power of 2 less than 9 is 8.

Input: N = -20
Output: -32
Explanation:
Highest power of 2 less than -20 is -32.

Input: N = -84
Output: -128

Approach: The idea is to use logarithm to solve the above problem. For any given number N, it can be either positive or negative. The following task can be performed for each case:

1. If the input is positive: floor(log2(N)) can be calculated.
2. If the input is negative: ceil(log2(N)) can be calculated and a -ve sign can be added to the value.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach` `#include ``using` `namespace` `std;` `// Function to return the lowest power``// of 2 close to given positive number``int` `powOfPositive(``int` `n)``{``    ``// Floor function is used to determine``    ``// the value close to the number``    ``int` `pos = ``floor``(log2(n));``    ``return` `pow``(2, pos);``}` `// Function to return the lowest power``// of 2 close to given negative number``int` `powOfNegative(``int` `n)``{``    ``// Ceil function is used for negative numbers``    ``// as -1 > -4. It would be opposite``    ``// to positive numbers where 1 < 4``    ``int` `pos = ``ceil``(log2(n));``    ``return` `(-1 * ``pow``(2, pos));``}` `// Function to find the highest power of 2``void` `highestPowerOf2(``int` `n)``{` `    ``// To check if the given number``    ``// is positive or negative``    ``if` `(n > 0) {``        ``cout << powOfPositive(n);``    ``}``    ``else` `{``        ``// If the number is negative,``        ``// then the ceil of the positive number``        ``// is calculated and``        ``// negative sign is added``        ``n = -n;``        ``cout << powOfNegative(n);``    ``}``}` `// Driver code``int` `main()``{``    ``int` `n = -24;``    ``highestPowerOf2(n);` `    ``return` `0;``}`

## Java

 `// Java implementation of the above approach``class` `GFG``{``    ` `    ``// Function to return the lowest power``    ``// of 2 close to given positive number``    ``static` `int` `powOfPositive(``int` `n)``    ``{``        ``// Floor function is used to determine``        ``// the value close to the number``        ``int` `pos = (``int``)Math.floor((Math.log(n)/Math.log(``2``)));``        ``return` `(``int``)Math.pow(``2``, pos);``    ``}``    ` `    ``// Function to return the lowest power``    ``// of 2 close to given negative number``    ``static` `int` `powOfNegative(``int` `n)``    ``{``        ``// Ceil function is used for negative numbers``        ``// as -1 > -4. It would be opposite``        ``// to positive numbers where 1 < 4``        ``int` `pos = (``int``)Math.ceil((Math.log(n)/Math.log(``2``)));``        ``return` `(``int``)(-``1` `* Math.pow(``2``, pos));``    ``}``    ` `    ``// Function to find the highest power of 2``    ``static` `void` `highestPowerOf2(``int` `n)``    ``{``    ` `        ``// To check if the given number``        ``// is positive or negative``        ``if` `(n > ``0``)``        ``{``            ``System.out.println(powOfPositive(n));``        ``}``        ``else``        ``{``            ``// If the number is negative,``            ``// then the ceil of the positive number``            ``// is calculated and``            ``// negative sign is added``            ``n = -n;``            ``System.out.println(powOfNegative(n));``        ``}``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `n = -``24``;``        ``highestPowerOf2(n);``    ``}``}` `// This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the above approach``using` `System;` `class` `GFG``{``    ` `    ``// Function to return the lowest power``    ``// of 2 close to given positive number``    ``static` `int` `powOfPositive(``int` `n)``    ``{``        ``// Floor function is used to determine``        ``// the value close to the number``        ``int` `pos = (``int``)Math.Floor((Math.Log(n)/Math.Log(2)));``        ``return` `(``int``)Math.Pow(2, pos);``    ``}``    ` `    ``// Function to return the lowest power``    ``// of 2 close to given negative number``    ``static` `int` `powOfNegative(``int` `n)``    ``{``        ``// Ceil function is used for negative numbers``        ``// as -1 > -4. It would be opposite``        ``// to positive numbers where 1 < 4``        ``int` `pos = (``int``)Math.Ceiling((Math.Log(n)/Math.Log(2)));``        ``return` `(``int``)(-1 * Math.Pow(2, pos));``    ``}``    ` `    ``// Function to find the highest power of 2``    ``static` `void` `highestPowerOf2(``int` `n)``    ``{``    ` `        ``// To check if the given number``        ``// is positive or negative``        ``if` `(n > 0)``        ``{``            ``Console.WriteLine(powOfPositive(n));``        ``}``        ``else``        ``{``            ``// If the number is negative,``            ``// then the ceil of the positive number``            ``// is calculated and``            ``// negative sign is added``            ``n = -n;``            ``Console.WriteLine(powOfNegative(n));``        ``}``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``int` `n = -24;``        ``highestPowerOf2(n);``    ``}``}` `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 implementation of the above approach``from` `math ``import` `floor,ceil,log2` `# Function to return the lowest power``# of 2 close to given positive number``def` `powOfPositive(n) :` `    ``# Floor function is used to determine``    ``# the value close to the number``    ``pos ``=` `floor(log2(n));``    ``return` `2``*``*``pos;` `# Function to return the lowest power``# of 2 close to given negative number``def` `powOfNegative(n) :` `    ``# Ceil function is used for negative numbers``    ``# as -1 > -4. It would be opposite``    ``# to positive numbers where 1 < 4``    ``pos ``=` `ceil(log2(n));``    ` `    ``return` `(``-``1` `*` `pow``(``2``, pos));` `# Function to find the highest power of 2``def` `highestPowerOf2(n) :` `    ``# To check if the given number``    ``# is positive or negative``    ``if` `(n > ``0``) :``        ``print``(powOfPositive(n));` `    ``else` `:``        ` `        ``# If the number is negative,``        ``# then the ceil of the positive number``        ``# is calculated and``        ``# negative sign is added``        ``n ``=` `-``n;``        ``print``(powOfNegative(n));` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``n ``=` `-``24``;``    ``highestPowerOf2(n);` `# This code is contributed by AnkitRai01`

## Javascript

 ``
Output:
`-32`

Attention reader! Don’t stop learning now. Participate in the Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students.

My Personal Notes arrow_drop_up