0

The result of this if statement was asked in my final exam. Afterwards, I searched for this sort of If statement but couldn't find it. Here's an example of this if statement. I experimented with it and apparently it works in mysterious ways :D

int a=1, b=1;
if(a != b >> 1)
{
    System.out.println("ABC");
}
Anas
  • 23
  • 5
  • 1
    It's comparing `a` and `(b >> 1)` (bitshift right once), which is the same as `b / 2`. And since `b` is an `int` (and `1`), that is `0`. So, `if 1 != 0`... thus it will print `ABC`. – Elliott Frisch Jun 23 '17 at 01:16
  • Right shift operator –  Jun 23 '17 at 01:17
  • The operations (>>) will be first later (!=). Here you can read more about the priority of the logic operations: http://introcs.cs.princeton.edu/java/11precedence/ and here: https://www.programiz.com/java-programming/operator-precedence – Vasyl Lyashkevych Jun 23 '17 at 01:35

0 Answers0