Java – why does 10 > > 2 5 > > 2 evaluate to zero?
•
Java
Consider the following codes:
int a=10,b=5; int c=a>>2+b>>2; System.out.println(c);
At run time, the (surprising) output is 0
Why is that?
Solution
Taking into account Java's operator precedence (especially precedence over > >) and association rules, the expression is equivalent to
(a >> (2 + b)) >> 2
or
(10 >> (2 + 5)) >> 2
This is zero
If you need to convert before adding, enclose them:
(a >> 2) + (b >> 2)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码