Comparing two integers in : java : Actally i am in pretty interesting problem when i run this method with n = 64, its gives me x = 64 but beside running the if statement it runs the else statement. the actal answer need to be 32 but it return 64.
/**
 * Complete the method to find the largest power of 2 less than the given number
 * Use a loop
 */

public class MathUtil
{
    public int largestPowerOf2(int n)
    {
        //TODO: implement this method.
        int i = 0;
        while(n > 1)
        {
            n = n / 2;
            i++;
        }
        System.out.printf("i = %d\n" , i);
        int x = (int)Math.pow(2,i);
        System.out.println(x);
        if(x == n)
        {
             return (int)Math.pow(2,i - 1);
        }
        else
        {
          return (int)Math.pow(2, i);
        }
    }
}