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);
    }
}
-2147483648 and 1
0x80000000 and 0x00000001
-2147483648 and -1
1 and -2147483648
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 1 of 2.

Sangeetha said:   7 years ago
Thanks for your explanation @Fakru.
(1)

Fakru said:   8 years ago
If you convert 0x80000000 (which is in hexadecimal form) to decimal form, it gives 2147483648. But, the range of integer in java is -2147483648 to 2147483647.

Since, the converted decimal value exceeds the range(positive value) of integer, it takes the negative value and the values go in a cycle from positive to negative. As the converted decimal value exceeds the integer range by 1, the 1st value from -2147483648 is taken, which is -2147483648.
(1)

Divya said:   9 years ago
Can anyone please explain it in detail?

Manisha said:   7 years ago
How to convert 0x80000000 in hexadecimal to decimal?
Can you explain the process?
What is the value of x?

Abhi said:   8 years ago
How can we say 0x80000000 as hexadecimal value?

LIYAKAT said:   8 years ago
It's really helpful. Thanks.

Nagalakshmi said:   9 years ago
How to convert hexadecimal to decimal such huge number very quickly?

Please give me the short method.

Shubham said:   9 years ago
Can anyone explain clearly how this answer came?

Amar said:   9 years ago
But hexadecimal representation of Number allow only 4 Digits after 0X?

Santhosh said:   1 decade ago
How do you perform shift operation? Can you explain?

How did you get -2147483648 and 1

what is mean by it ?


Post your comments here:

Your comments will be displayed after verification.