File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bitwise Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bitwise Operators" Watch "Bitwise Operators" New topic

Bitwise Operators

sudha siva
Ranch Hand

Joined: Sep 18, 2001
Posts: 30
Hai everyone,
class Test{
public static void main(String args[]){
int x=0;
boolean b1,b2,b3,b4;
x=(b1 | b2 & b3 ^b4) ? x++ : --x; // line 1

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.

Simeon Shi
Ranch Hand

Joined: Dec 09, 2001
Posts: 35
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
Philip Hung

Joined: Dec 11, 2001
Posts: 10
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.
vishal avad
Ranch Hand

Joined: Nov 29, 2001
Posts: 45
hi sudha siva
i dont think it has any relation with bitwise 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.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
Hi Sudha
operators' priority is & -> ^ -> |
it is evaluated as (b1 | ((b2 & b3) ^b4))
b2 & b3 = true
true ^ b4 = false
b1 | false = true
hence x = x++;
but now I am confused ??


"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

Joined: Jun 14, 2001
Posts: 126
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
The value 0 is assigned to the variable x
Hope this helps...
I agree. Here's the link:
subject: Bitwise Operators
It's not a secret anymore!