sudha siva

Ranch Hand

Posts: 30

posted 15 years ago

Hai everyone,

class Test{

public static void main(String args[]){

int x=0;

boolean b1,b2,b3,b4;

b1=b2=b3=b4=true;

x=(b1 | b2 & b3 ^b4) ? x++ : --x; // line 1

System.out.println(x);

}

}

The output of the above code is 0.And in the answer the line 1 is evaluated as (b1 | (b2 & (b3 ^ b4)).But the AND operator should be evaluated first in this type of expression.(I think i,m correct).I'm confused of this code.Can anyone clear my doubt.

Thanks

sudha

class Test{

public static void main(String args[]){

int x=0;

boolean b1,b2,b3,b4;

b1=b2=b3=b4=true;

x=(b1 | b2 & b3 ^b4) ? x++ : --x; // line 1

System.out.println(x);

}

}

The output of the above code is 0.And in the answer the line 1 is evaluated as (b1 | (b2 & (b3 ^ b4)).But the AND operator should be evaluated first in this type of expression.(I think i,m correct).I'm confused of this code.Can anyone clear my doubt.

Thanks

sudha

Simeon Shi

Ranch Hand

Posts: 35

Philip Hung

Greenhorn

Posts: 10

vishal avad

Ranch Hand

Posts: 45

posted 15 years ago

Hi Sudha

operators' priority is & -> ^ -> |

it is evaluated as (b1 | ((b2 & b3) ^b4))

so

b2 & b3 = true

true ^ b4 = false

b1 | false = true

hence x = x++;

but now I am confused ??

------------------

Regards

Ravish

operators' priority is & -> ^ -> |

it is evaluated as (b1 | ((b2 & b3) ^b4))

so

b2 & b3 = true

true ^ b4 = false

b1 | false = true

hence x = x++;

but now I am confused ??

------------------

Regards

Ravish

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh

Uma Viswanathan

Ranch Hand

Posts: 126

posted 15 years ago

x=(b1 | b2 & b3 ^b4) ? x++ : --x;

First let us evaluate

true | ( (true & true ) ^ true )

i.e., true | (true ^ true )

i.e., true | false

i.e., true

Now, the expression is equivalent to

x = x++;

The steps are

1) Return the value of x

2) Increment the value of x

3) Assign the value returned by step 1 to L.H.S variable

So,

The value 0 is assigned to the variable x

Hope this helps...

Uma

First let us evaluate

true | ( (true & true ) ^ true )

i.e., true | (true ^ true )

i.e., true | false

i.e., true

Now, the expression is equivalent to

x = x++;

The steps are

1) Return the value of x

2) Increment the value of x

3) Assign the value returned by step 1 to L.H.S variable

So,

The value 0 is assigned to the variable x

Hope this helps...

Uma