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
- Which of the following HTML5 codes will affect the horizontal as well as vertical alignment of the table content?
- Consider the C/C++ function f() given below: void f(char w[]) { int x = strlen(w); // length of the string char c; for (int i = 0; i < x; i…
- The definitions in an XML document are said to be __________ when the tagging system and definitions in the DTD are all in compliance.
- Consider the JavaScript code: var y = "12"; function f() { var y = "6"; alert(this.y); function g() { alert(y); } g(); } f(); If M is the n…
- What is the output of the following Java program? class simple { public static void main(String[] args) { simple obj = new simple(); obj.st…
- What is the output of the following C program? (Assume little-endian representation, with the least significant byte at the lowest address,…