2

Here the code.

public class Test {

    public static void main(String[] args) {
        int x=2; int y = 3;
        if((y == x++) | (x < ++y)){
            System.out.println("x = " + x + "Y = " + y);
        }
    }
}

This outputs x=3 y=4

Another Criteria

public class Test {

    public static void main(String[] args) {
        System.out.println(4|3);
    }
}

This outputs 7

Here the second criteria | works as bit wise operator. But in first criteria | is not. It manipulates and giving the output. I need explanation how the first criteria works.

The thing i know is, | will compare both sides and || will compare only left side and it true it proceeds to next.

Hope my question is clear. Thankyou in advance..

Java Beginer
  • 321
  • 2
  • 16
  • http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html – fGo Jun 18 '13 at 08:11
  • I went to one interview today and the second criteria was asked there. I marked 0 as answer by thinking no proper boolean experession. After reaching home this makes me confused. How this operator can take two work. bit wise or and or operations. – Java Beginer Jun 18 '13 at 08:13
  • Also see [Differences in boolean operators: & vs && and | vs ||](http://stackoverflow.com/questions/4014535/differences-in-boolean-operators-vs-and-vs) – devnull Jun 18 '13 at 08:20

3 Answers3

2

For the first example you will have:

if ((3 == 2) | (3 < 4))

which is equal to:

if ( false | true )

and evaluates to true (since the both sides of the expression are compared).

For the second example, since you already know that | will compare both sides, here's what it really happens. The numbers are converted into binary ones and then the operation is applied:

4 | 3

will be converted to:

100 | 011 = 111

which is equal to 7.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
1

Follow the JLS 15.22.2:

When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. In all cases, the operands are subject to unboxing conversion (§5.1.8) as necessary.

For &, the result value is true if both operand values are true; otherwise, the result is false.

For ^, the result value is true if the operand values are different; otherwise, the result is false.

For |, the result value is false if both operand values are false; otherwise, the result is true.

Take the first case:

int x=2; int y = 3;
if((y == x++) | (x < ++y)){
       System.out.println("x = " + x + "Y = " + y);
}

(y == x++) is false because x++ is post-increment , (x < ++y) is true , hence false|true is true and you get the console o/p with the values of x=3 and y=4.


Second case , is natural bitwise OR :

System.out.println(4|3);

That in 3-bits is (011|100) = (111) = 7(base 10).

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • Very nice explanation buddy. Thankyou. Now i can understand how it works. Any how it is taking boolean values as it operands and manipulates. – Java Beginer Jun 18 '13 at 08:17
1

In the first example, first the left side of the expression is evaluated:

y == x++ // y == x; x=x + 1

y is 3, x is 2, the equality check is false, and the x is incremented to 3.

and then the right side of the expression is evaluated:

x < ++y; // y = y + 1; x < y

y is incremented and is 4, and then is checked if x is smaller than y. And yes, 3 is smaller than 4, and the result of the right part is true.

Since there is bitwise | operator between false on the left side and true on the right side, the result is

false | true = true

And that's why the expression is true and syso is executed.


In the second example, a bitwise OR is executed, hence

100 | 011 = 111 //is equal to 7
darijan
  • 9,725
  • 25
  • 38