English

What gets displayed on the screen when the following program is compiled and run. Select the one correct answer. public class test{ public static void main(String args[]){ int x; x = -3 >> 1;

Advertisements

Question

What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.

public class test{
public static void main(String args[]){
int x;
x = -3 >> 1;
x = x >>> 2;
x = x<<1;
System.out.println(x);
}
}
Long Answer
Advertisements

Solution

Initially, x = −3

1. x = -3 >> 1;

The signed right shift operator (>>) shifts the bits one position to the right while preserving the sign.

x = −2

2. x = x >>> 2;

The unsigned right shift operator (>>>) shifts the bits two positions to the right and fills the leftmost bits with zeros.

x = 1073741823

3. x = x << 1;

The left shift operator (<<) shifts the bits one position to the left.

x = 2147483646

Output:

2147483646
shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Variables and Expressions - EXERCISE [Page 134]

APPEARS IN

Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 6 Variables and Expressions
EXERCISE | Q 55. | Page 134
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×