Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to write a Java method that accepts a long for a parameter then uses

ID: 3623021 • Letter: I

Question

I'm trying to write a Java method that accepts a long for a parameter then uses this parameter to compare the closest, yet lowest possible power. This must be returned and stored into memory.

For example, say if the parameter was 133, the closest power of 2 would be 128, which is 2^8. The 8 would be stored into memory. Here's the code that I have so far:

public static int highestPowerOf2(long x){
int highestPower = 0;
for (int i=0; i<x; i^2){
highestPower = (int) Math.pow(i, 2);
}
return highestPower;
}

Explanation / Answer

// save as po.java and run dude :)

public class po {

    public static int highestPowerOf2(long x){
    int highestPower = 0;
    while(x>0)
    {
    highestPower++;
    x = x/2;
    }
    return highestPower;
    }
    public static void main(String[] args) {
   
    System.out.println(highestPowerOf2(131));
    }

}