info@meritcampus.com    +91-85006-22255
...
`

Bit Shift
What will be the output of the following program?
`public class BitShift {    public static void main(String[] args) {        int num = 0x80000000;        System.out.print(num + " and  ");        num = num >>> 31;        System.out.println(num);    }}`

 A. -2147483648 and 1 B. 0x80000000 and 0x00000001 C. 2147483648 and -1 D. 1 and -2147483648 E. Compilation Error or Runtime Error
Topic:

### User comments below.All of them might not be correct.

ans: A

Posted by Sapparapu Pradeep Kumar    2014-03-05 04:18:47

ans:A because here the value contain in the foramat of 32 bit  hexcadecimal so.the value in the num is  like -2147483648 and then >>> is used for shift operation to be shifted using shift operations so..we get output 1 so ans:A

Posted by Sapparapu Pradeep Kumar    2014-03-05 04:25:35

ans is A 0x is hexadecimal format and so the num contains -2147483648 on using >>>  The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.so the result in num is 1;

Posted by Sandeep Badvel    2014-03-05 06:50:48

Answer is A. Numeric types in Java store only the values, not any particular representation of the values. It doesn't make any difference whether they are initialized from octal or hexadecimal or decimal literals. Java stores signed integers in 2's complement form. That means, in the whole 2^32 possible values, +ve numbers start with a 0-bit and -ve numbers with a 1-bit. Now, 0x80000000 = 1000 0000 0000 0000 0000 0000 0000 0000 (maximum of 32-bits). It's a -ve number as it is starts with a 1-bit. Therefore, 0x80000000 = -2147483648. Then, with num >>> 31, num is right shifted by 31-bits (unsigned right shift >>> will shift the sign bit too, replacing it with a 0-bit). If it was num >> 1 (normal right shift), the answer would be -1 (because it retains the sign bit).

Posted by Shaileshwar Sharma    2014-03-05 09:59:50

Congratulations Sandeep Badvel. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-03-06 04:59:08