Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 4)
4.
What will be the output of the program?
class BitShift
{
public static void main(String [] args)
{
int x = 0x80000000;
System.out.print(x + " and ");
x = x >>> 31;
System.out.println(x);
}
}
Answer: Option
Explanation:
Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this:
Before: 1000 0000 0000 0000 0000 0000 0000 0000
After: 0000 0000 0000 0000 0000 0000 0000 0001
Option C is incorrect because the >>> operator zero fills the left bits, which in this case changes the sign of x, as shown.
Option B is incorrect because the output method print() always displays integers in base 10.
Option D is incorrect because this is the reverse order of the two output numbers.
Discussion:
17 comments Page 2 of 2.
Keshav Kumar said:
9 years ago
Binary equivalent of 0X80000000 is 10000000000000000000000000000000. In int data type if 32nd bit is off, then decimal equivalent is positive and if 32nd bit is on then the equivalent is negative.
Bala phani said:
9 years ago
How to perform shift operation? Please explain me.
Ricky said:
9 years ago
0X80000000 to 2147483648(2^31) is clear but how this '-' sign comes in it?
Sriram said:
10 years ago
How the 0000 0000 0000 0000 0000 0000 0000 0001 converts to -2147483648 and 1?
Hima said:
10 years ago
int x = 0x80000000;
'x' in the value 0x80000000; indicates it as a hexadecimal value.
'x' in the value 0x80000000; indicates it as a hexadecimal value.
Satyam said:
1 decade ago
How you can say that it is in hexadecimal form?
Suresh Bandam said:
1 decade ago
Int x = 0x80000000;
Here, x value is in Hexadecimal and if we convert into in decimal -2147483648.
If we convert into in binary, the value is 1000 0000 0000 0000 0000 0000 0000 0000.
x = x >>> 31;
X is stored using 32 bit 2's complement form.
By this Unsigned right shift operation,
Before: 1000 0000 0000 0000 0000 0000 0000 0000.
After: 0000 0000 0000 0000 0000 0000 0000 0001.
Here, x value is in Hexadecimal and if we convert into in decimal -2147483648.
If we convert into in binary, the value is 1000 0000 0000 0000 0000 0000 0000 0000.
x = x >>> 31;
X is stored using 32 bit 2's complement form.
By this Unsigned right shift operation,
Before: 1000 0000 0000 0000 0000 0000 0000 0000.
After: 0000 0000 0000 0000 0000 0000 0000 0001.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers