Java Programming - Operators and Assignments - Discussion

Discussion Forum : Operators and Assignments - Pointing out the correct statements (Q.No. 3)
3.
Which two statements are equivalent?
  1. 16*4
  2. 16>>2
  3. 16/2^2
  4. 16>>>2
1 and 2
2 and 4
3 and 4
1 and 3
Answer: Option
Explanation:

(2) is correct. 16 >> 2 = 4

(4) is correct. 16 >>> 2 = 4

(1) is wrong. 16 * 4 = 64

(3) is wrong. 16/2 ^ 2 = 10

Discussion:
27 comments Page 1 of 3.

Jose Marti said:   1 decade ago
Because the unsigned right shift operator ">>>" shifts a ZERO into the leftmost position.

In binary 16 is 10000, now shift the bits two places to the right. We get 100. Then add a 0 into the leftmost position, we get 0100 which is 4.
(2)

Khan said:   7 years ago
16>>2 signed right shift.

16>>>2 unsigned right shift. The most significant bit in >> is sign bit, so that if the sign bit is 0, the number is positive and if the sign bit is 1 the number is negative.

Here, since the number is already positive when we shift by >>, we get 100 and when we use >>> we get 0100, the 0 before 1 represents that its a positive number. Had this been -16, the answer would have been 100 for >> and 1100 for >>>.
(1)

Utpal Sharma said:   1 decade ago
In java >> and >>> both have same meaning right shift and 16/2^2 here first 16/2=8 and then 8^2 perform bitwise OR operation so 8 in binary 1000, 2 is 0010 output is 1010 which is 10.
(1)

Asmat said:   1 decade ago
16>>2 signed right shift.

16>>>2 unsigned right shift.. the most significant bit in >> is sign bit, so that if the sign bit is 0, the number is positive and if the sign bit is 1 the number is negative.

Here since the number is already positive when we shift by >>, we get 100 and when we use >>> we get 0100, the 0 before 1 represents that its a positive number..Had this been -16, the answer would have been 100 for >> and 1100 for >>>.

Allauddin Pirjade said:   8 years ago
In simple words >>> always shifts a zero into the leftmost position whereas >> shifts based on sign of the number i.e. 1 for negative number and 0 for positive number.

Neethu said:   8 years ago
16/2^2.
16/2=8->1000.
1000^0010=1010~=10.

Gowthami said:   8 years ago
Answer is 16.

Santhosh said:   9 years ago
2<<3 what is the answer, can you explain me?

Priyanka Srivastava said:   9 years ago
Sir, please give the explanation about how 16 >>> 2 is equals 16 >> 2.

I do not understand the question.

Sumit said:   10 years ago
Hi, how this is possible?


Post your comments here:

Your comments will be displayed after verification.