Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

bitwise operators priority--from Jimmy's mock exam

Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Hi
I think the questions about operators in Jimmy are sort of tough. I was lost. See the examples plz.
I think &,|,^ are same in priority, and I can process from left to right. THe result: I got all questions below wrong.
I have given the output the explanation provided by the author, but it makes me more confused, when one evaluation is in
(b1(b2&(b3^b4))???
and another is in
(8|((9&10)^11)) ???
How come there is differences?
Thanks for the help.
CHris
public class Op
{
public static void main(String[] s){
boolean b1,b2,b3,b4;
b1=b2=b3=b4=true;
int m,n;
System.out.println(b1|b2&b3^b4?"true":"false");
//output : true (b1(b2&(b3^b4))???
m=8|9&10^11;//(8|((9&10)^11)) ???
System.out.println("m is "+m);
//output:11
n=7;
n<<=3;<br /> n=n&n+1|n+2^n+3;<br /> n>>=2;
System.out.println("n is "+n);
//output 14
}
}

corneilguy
Greenhorn

Joined: Feb 03, 2001
Posts: 12
Order of bitwise ops is &, then ^, then
corneilguy
Greenhorn

Joined: Feb 03, 2001
Posts: 12
Order of bitwise ops is &, then ^, then vertical bar
corneilguy
Greenhorn

Joined: Feb 03, 2001
Posts: 12
And, of course, + precedes even &
corneilguy
Greenhorn

Joined: Feb 03, 2001
Posts: 12
~ precedes & but follows arithmetic ops
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Thanks. &^| sequence does work in all three cases. However, I read RHE about bitwise operator, my impression is the those three are equal. Even worse, my overall impression is the sequence has to be from left to right (after reading the discussion about short-circuit).
If possible, may I have a reference about this issue? I was lost in JLS.
Thanks a lot
Chris
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
In addition, the author give one answer as
(b1|(b2&(b3^b4))
though no difference in result, but now it is ^&|

I agree. Here's the link: http://aspose.com/file-tools

subject: bitwise operators priority--from Jimmy's mock exam
Similar Threads
From mock exam about Autoboxing
Unable to understand result of this code
Bitwise Operators
Logical operators precedence
Operator precedence