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.
hi,sudha... u r right about the operators' priority--&->^->|, then the result of (b1 | b2 & b3 ^b4) is true. okay, the next step is x = x++; yes,it is where u confused. such code as x=x--;x=x++; won't change the the value of x. so x still is 0; the output should be 0
The Operator Precedence would be & then ^ then |. So b2 & b3 gets evaluated first (results to true), then ^ b4 (results to false) lastly | b1 (results to true). Evaluate x++ which returns 0.
hi sudha siva i dont think it has any relation with bitwise operator.it is has problem with increment operator for ref. see post increment operator posted by ravish kumar i m also waiting 4 its answer. its really complicated. rgds vishal
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
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
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
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.