# Highest power of 2 less than or equal to a given number

## Highest power of 2 less than or equal to a given number

Problem Statement : You are given with a number you need to find the highest power of 2 either less than or equal to the given number.

So, for this problem first we will se the iterative method to solve this problem.

Method 1:

Code Solution Java:

```//Highest power of 2 less than or equal to a given number in java

class CodeSagar {
static int HighestPowerof2(int n){

int f=1;
while(f*2<=n){
f=f*2;
}
return f;
}
public static void main(String[] args){
int num=100;
System.out.println(HighestPowerof2(num));
}
}

//Highest power of 2 less than or equal to a given number java```

Method 2:

In this approach we will use log to get the exponent value and then we will compute the power of two.

```//Highest power of 2 less than or equal to a given number solution

import java.lang.*;
class CodeSagar {
static int HighestPowerof2(int n){
return (int)Math.pow(2,(int)(Math.log(n) / Math.log(2)));
}
public static void main(String[] args){
int num=10;
System.out.println(HighestPowerof2(num));
}
}

//Highest power of 2 less than or equal to a given number java solution```