Java Programming - Operators and Assignments - Discussion

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);
    }
}

[A]. -2147483648 and 1
[B]. 0x80000000 and 0x00000001
[C]. -2147483648 and -1
[D]. 1 and -2147483648

Answer: Option A

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.


Santhosh said: (Sep 5, 2013)  
How do you perform shift operation? Can you explain?

How did you get -2147483648 and 1

what is mean by it ?

Suresh Bandam said: (Jan 28, 2015)  
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.

Satyam said: (Oct 7, 2015)  
How you can say that it is in hexadecimal form?

Hima said: (Nov 7, 2015)  
int x = 0x80000000;

'x' in the value 0x80000000; indicates it as a hexadecimal value.

Sriram said: (Nov 18, 2015)  
How the 0000 0000 0000 0000 0000 0000 0000 0001 converts to -2147483648 and 1?

Ricky said: (Aug 23, 2016)  
0X80000000 to 2147483648(2^31) is clear but how this '-' sign comes in it?

Bala Phani said: (Aug 29, 2016)  
How to perform shift operation? Please explain me.

Keshav Kumar said: (Nov 14, 2016)  
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.

Divya said: (Nov 20, 2016)  
Can anyone please explain it in detail?

Amar said: (Dec 2, 2016)  
But hexadecimal representation of Number allow only 4 Digits after 0X?

Shubham said: (Jan 17, 2017)  
Can anyone explain clearly how this answer came?

Nagalakshmi said: (Feb 3, 2017)  
How to convert hexadecimal to decimal such huge number very quickly?

Please give me the short method.

Fakru said: (May 9, 2017)  
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.

LIYAKAT said: (Jul 5, 2017)  
It's really helpful. Thanks.

Abhi said: (Sep 6, 2017)  
How can we say 0x80000000 as hexadecimal value?

Manisha said: (Jul 15, 2018)  
How to convert 0x80000000 in hexadecimal to decimal?
Can you explain the process?
What is the value of x?

Sangeetha said: (Sep 21, 2018)  
Thanks for your explanation @Fakru.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.