I think the answer is straight forward but still I am not getting it.
byte a=5;
int b=10;
int c=a>>2+b>>2;
System.out.print(c);
As a>>2 is 1 and b>>2 is 2, I am expecting output to be 3 but is 0. What's the reason?
It's because of operator precedence.
What you do is the same as
int c=(a>>(2+b))>>2;
You want this :
int c=(a>>2)+(b>>2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With