All mock tests are FREE till 31 December — register to save your progress

What does the following Java function perform? (Assume int occupies four bytes of storage) public static int f(

int a)
{ // Pre-conditions : a>0 and no
overflow/underflow occurs int b=0 ;
for (int i=0; i<32; i++)
{
b=b<<1 ;
b=b | (a & 1);
a=a≫>1;// This is a logical shift
}
return b;
}

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: B. Return the int that has the reversed binary representation of integer a.

Explanation

Each iteration shifts b left and appends a's lowest bit, then shifts a right: after 32 iterations b holds a's bits in reverse order.

More Programming Languages and Computer Graphics questions