Java Loops II

  • + 1 comment

    This line:

    int ans=a+1*b;
    

    Should be:

    int ans = a;
    

    Since you're loop from 0 you code is calculating a + 2^0*b twice and you will get a wrong result.

    And this line:

    int x=2^j*b;
    

    The '^' it xor (exclusive or) operator not pow operator. You have to use Math.pow(); or a shift operation.