-5

I need explanation of the below code execution

public static void main(String[] args) {
    Long tail = 2000L;
    Long distance = 1999L;
    Long story = 1000L;
    if ((tail > distance) ^ ((story * 2) == tail))
        System.out.println("a");
    if ((distance + 1 != tail) ^ ((story * 2) == distance))
        System.out.println("2");

}

After the code execution no output is produced.

Sandeep
  • 2,219
  • 2
  • 15
  • 13

7 Answers7

5

^ stands for XOR operation of 2 booleans, in below statement

if ((tail > distance) ^ ((story * 2) == tail))

(tail > distance) is true

((story * 2) == tail) is true

and true XOR true is false

Community
  • 1
  • 1
sanbhat
  • 17,522
  • 6
  • 48
  • 64
3

the XOR output table looks like this:
X Y Output
0 0 0
1 0 1
0 1 1
1 1 0

So based on both your booleans being true you will get a false return from the if statements.

Rushfire
  • 56
  • 3
1
public static void main(String[] args) {
    Long tail = 2000L;
    Long distance = 1999L;
    Long story = 1000L;
    if ((tail > distance) ^ ((story * 2) == tail))
        System.out.println("a");
    if ((distance + 1 != tail) ^ ((story * 2) == distance))
        System.out.println("2");
}

if (((tail > distance) ^ ((story * 2) == tail)) == true)
            System.out.println("a");

is equal to:

if (((2000 > 1999) ^ ((1000 * 2) == 2000)) == true)
            System.out.println("a");

is equal to:

if ((true ^ true) == true)
            System.out.println("a");

is equal to:

if (false == true)
            System.out.println("a");

and therefor it's never printed.

Similar reasoning applies to the second if-else statement.

ioreskovic
  • 5,531
  • 5
  • 39
  • 70
0

Both your conditions are false thats why you have no output

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
0

You never have anything returned as there isn't anything to return - both conditions are false.

Also I'm fairly sure it should be written as

if ((tail > distance) ^ ((story * 2) == tail)) {
    System.out.println("a");
}
if ((distance + 1 != tail) ^ ((story * 2) == distance)) {
    System.out.println("2");
}

You could also test it by saying

if ((tail > distance) ^ ((story * 2) == tail)) {
    System.out.println("a");
} else {
    System.out.println("Condition 1 False");
}
if ((distance + 1 != tail) ^ ((story * 2) == distance)) {
    System.out.println("2");
} else {
    System.out.println("condition 2 False");
}

Alternatively, did you mean to write

if ((tail > distance) || ((story * 2) == tail)) {
    System.out.println("a");
}
if ((distance + 1 != tail) || ((story * 2) == distance)) {
    System.out.println("2");
}

Using the logical OR instead?

Chris Byatt
  • 3,689
  • 3
  • 34
  • 61
0

You may be missing a basic understanding of the difference between 'or' and 'exclusive-or'. The exclusive-or condition a ^ b is true only if (a is true and b is false) OR (a is false and b is true).

fred02138
  • 3,323
  • 1
  • 14
  • 17
0

^ is XOR. Which is almost the opposite of OR.

OR:

True True = True
True False = False
False True = False
False False = False

XOR:

True True = False
True False = True
False True = True 
False False = False
Arash Saidi
  • 2,228
  • 20
  • 36