Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

public class Case54
{
    public static void main(String[] args) throws Exception
    {
        int flags = 20;
        int top = Integer.highestOneBit(flags);
        int bottom = Integer.lowestOneBit(flags);
        System.out.println("top=" + top);
        System.out.println("bottom=" + bottom);
    }
}

  • A.top=4 then bottom=16
  • B.top=16 then bottom=4
  • C.top=20 then bottom=1
  • D.top=16 then bottom=1

Answer: B

Correct answer: top=16 then bottom=4

Twenty in binary is 10100. highestOneBit returns the value of the most significant set bit, which is 16, and lowestOneBit the value of the least significant set bit, which is 4. Both return the *value* of the bit rather than its position, which is why the answer is 16 and 4 rather than 4 and 2.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.