52.9k views
2 votes
Part 3a. Largest power of two less than (10 pts) Implement the method public static int largestPow2LessThan(int n) which calculates and returns the largest integer power of 2 less than n. You may assume n is greater than 1. For example, if n is 10, the largest power of 2 less than n is 8 (8 is 2 cubed). If n is 8, the answer is 4. If n is 2, 20

User Richardsun
by
4.8k points

1 Answer

7 votes

Answer:

Check the explanation

Step-by-step explanation:

========================================================================

// Part 3 (a)

public static int largestPow2LessThan(int n) {

int two = 1;

while (two * 2 < n) {

two *= 2;

}

return two;

}

================================================================

User Shivangam Soni
by
4.3k points