 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lonelyplanet999 Guest
|
Posted: Wed Dec 17, 2003 7:27 pm Post subject: Precedence of &, |, &&, || operators |
|
|
Hi,
I would like to know without separating parentheses '()', any
precedence among the 4 logical operators &, |, &&, || ?
I know &&, || being short circuit operator meaning (a && b) returns
false so long as 'a' evaluates to false and (a || b) returns true so
long as 'a' evaluates to true ?
That is, any difference in evalulation order such as below example
expression ?
(a & b | c & d | e) equivalent to ((((a&b) | c) & d) | e) ?
(a && b || c && d || e) equivalent to ((((a&&b) || c) && d) | e) ?
I read one book which says & has precedence over | as logical
operator, how about the precedence order between & and ||, | and && ?
I'm trying my best to test different combination of these expressions
but I'm afraid I still miss some
|
|
| Back to top |
|
 |
Alex Hunsley Guest
|
Posted: Wed Dec 17, 2003 9:41 pm Post subject: Re: Precedence of &, |, &&, || operators |
|
|
lonelyplanet999 wrote:
| Quote: | Hi,
I would like to know without separating parentheses '()', any
precedence among the 4 logical operators &, |, &&, || ?
|
Tried googling for it? Always a good port of call.
Found this:
http://highered.mcgraw-hill.com/sites/0072488190/student_view0/operator_precedence_chart.html
| Quote: |
I know &&, || being short circuit operator meaning (a && b) returns
false so long as 'a' evaluates to false and (a || b) returns true so
long as 'a' evaluates to true ?
|
Correct. This is called 'strict evaluation'.
[snip]
| Quote: | I'm trying my best to test different combination of these expressions
but I'm afraid I still miss some
|
It's a good idea to use brackets anyway, in order to make your code more
readable.
Simply missing out brackets on a complicated expression because you know
it does what you want isn't a good practise - use brackets to make
things clear.
alex
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Wed Dec 17, 2003 11:03 pm Post subject: Re: Precedence of &, |, &&, || operators |
|
|
"lonelyplanet999" <lonelyplanet999 (AT) my-deja (DOT) com> wrote
| Quote: | Hi,
I would like to know without separating parentheses '()',
any precedence among the 4 logical operators &, |,
&&, || ?
I know &&, || being short circuit operator meaning (a && b)
returns false so long as 'a' evaluates to false and (a || b) returns
true so long as 'a' evaluates to true ?
That is, any difference in evalulation order such as below
example expression ?
(a & b | c & d | e) equivalent to ((((a&b) | c) & d) | e) ?
|
No. It is:
(((a & b) | (c & d)) | e)
You would:
* Join the '&' sub-expressions, proceeding from left to right,
since they have the highest precedence
* Again, proceeding from left to right, join the sub-expressions
with '|'
| Quote: |
(a && b || c && d || e) equivalent to ((((a&&b) || c) && d) | e) ?
|
No. It is:
(((a && b) || (c && d)) || e)
If it were originally:
a && b || c && d | e
it would be:
((a && b) || (c && (d | e)))
| Quote: |
I read one book which says & has precedence over
| as logical operator, how about the precedence order
between & and ||, | and && ?
I'm trying my best to test different combination of these
expressions but I'm afraid I still miss some :)
|
Precedence rules for boolean logicals and condional operators:
------- Highest
&
^
| Quote: |
&&
|
------- Lowest |
In evaluating such expressions:
* Start on the left, and move towards the right
* Sub-expressions which are already grouped take
precedence
* As you move along, group sub-exprssions together
based on precedence rules
* Move back over the expression joining the sub-expressions
based on precedence rules
The annotated code below may be of help [please excuse any errors in the
annotations though I'm fairly sure there are none].
Anthony Borla
// ----------------------------------------
public class TestPrecedence
{
public static void main(String[] args)
{
// (true & true) ...
// ((true & true) ^ true) ...
// (((true & true) ^ true) | true)
boolean r1 = true & true ^ true | true;
// ... (true & true)
// ... (true ^ (true & true))
// (true | (true ^ (true & true)))
boolean r2 = true | true ^ true & true;
// ... (true & true)
// (true ^ true) ... (true & true)
// ((true ^ true) | (true & true))
boolean r3 = true ^ true | true & true;
// (true & true) ...
// ((true & true) ^ true) ...
// (((true & true) ^ true) | true) ...
// ((((true & true) ^ true) | true) && true) ...
// (((((true & true) ^ true) | true) && true) || true)
boolean r4 = true & true ^ true | true && true || true;
// ... (true & true)
// ... (true | (true & true))
// ... (true ^ true) ... (true | (true & true))
// (true && (true ^ true)) ... (true | (true & true))
// ((true && (true ^ true)) || (true | (true & true)))
boolean r5 = true && true ^ true || true | true & true;
// ... true & ...
// ... (true & ((1 ^ 1) == 0)) ...
// (true | (true & ((1 ^ 1) == 0))) ...
// ((true | (true & ((1 ^ 1) == 0))) | ((1 & 1) != 0))))
boolean r6 = true | true & ((1 ^ 1) == 0) | ((1 & 1) != 0);
}
}
// ----------------------------------------
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Thu Dec 18, 2003 11:04 am Post subject: Re: Precedence of &, |, &&, || operators |
|
|
On 17 Dec 2003 11:27:21 -0800, [email]lonelyplanet999 (AT) my-deja (DOT) com[/email]
(lonelyplanet999) wrote or quoted :
| Quote: | I would like to know without separating parentheses '()', any
precedence among the 4 logical operators &, |, &&, || ?
|
see http://mindprod.com/jgloss/precedence.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
lonelyplanet999 Guest
|
Posted: Sun Dec 21, 2003 4:36 pm Post subject: Re: Precedence of &, |, &&, || operators |
|
|
Roedy Green <look-at-the-website-for-actual-Roedy (AT) mindprod (DOT) com> wrote
| Quote: | On 17 Dec 2003 11:27:21 -0800, [email]lonelyplanet999 (AT) my-deja (DOT) com[/email]
(lonelyplanet999) wrote or quoted :
I would like to know without separating parentheses '()', any
precedence among the 4 logical operators &, |, &&, || ?
see http://mindprod.com/jgloss/precedence.html
|
Hi,
I've read through your list and compared it with my book's copy (Java
2 Certification All-in-One Exam Guide, Third Edition, by William
Stanek) and found below difference:
The book said operator precedence from high to low as
[], ()
++(Postfix), --(Postfix) # Your list listed Prefix ++/-- before
Postfix ++/--
++(Prefix), --(Prefix), +(Positive), -(Negative), !, ~
(Type)
= / %
+(Addition), -(Subtraction)
<<, >>, >>>
<, >, =>, <=, instanceof
==, !=
&
^
=, +=, -=, *=, /=, %=, &=, ^=, |=, >>=, <<=, >>>=
I couldn't think of real code example to test which version is correct
yet.
|
|
| Back to top |
|
 |
lonelyplanet999 Guest
|
Posted: Sun Dec 21, 2003 6:40 pm Post subject: Re: Precedence of &, |, &&, || operators |
|
|
Tks:>
I also found that if I write below lines,
i=1;
i<<=i++;
i will become 2 afterwards. That is only i<<=i where i is still 1
being executed, the ++ operation takes no effect on value of i.
Why will that happens ;(
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sun Dec 21, 2003 11:25 pm Post subject: Re: Precedence of &, |, &&, || operators |
|
|
On 21 Dec 2003 08:36:21 -0800, [email]lonelyplanet999 (AT) my-deja (DOT) com[/email]
(lonelyplanet999) wrote or quoted :
| Quote: | ++(Postfix), --(Postfix) # Your list listed Prefix ++/-- before
Postfix ++/--
|
prefix ++ is executed before post fix. But that is not what precedence
means.
If they are listed on the same line, that means they have the same
precedence.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Mon Dec 22, 2003 12:37 am Post subject: Re: Precedence of &, |, &&, || operators |
|
|
"lonelyplanet999" <lonelyplanet999 (AT) my-deja (DOT) com> wrote
| Quote: | Roedy Green <look-at-the-website-for-actual-Roedy (AT) mindprod (DOT) com> wrote in
message news:<8d23uvcjddc83mnaq98r7cmvnhkl3nm41h (AT) 4ax (DOT) com>...
On 17 Dec 2003 11:27:21 -0800, [email]lonelyplanet999 (AT) my-deja (DOT) com[/email]
(lonelyplanet999) wrote or quoted :
I would like to know without separating parentheses '()', any
precedence among the 4 logical operators &, |, &&, || ?
see http://mindprod.com/jgloss/precedence.html
Hi,
I've read through your list and compared it with my book's
copy (Java 2 Certification All-in-One Exam Guide, Third
Edition, by William Stanek) and found below difference:
The book said operator precedence from high to low as
[], ()
++(Postfix), --(Postfix) # Your list listed Prefix ++/-- before
Postfix ++/--
++(Prefix), --(Prefix), +(Positive), -(Negative), !, ~
(Type)
= / %
+(Addition), -(Subtraction)
, >>,
, >, =>, <=, instanceof
==, !=
&
^
|
&&
||
?:
=, +=, -=, *=, /=, %=, &=, ^=, |=, >>=, <<=, >>>=
I couldn't think of real code example to test which version
is correct yet.
|
Take a look at the sample code and decide for yourself which version is
correct !
I hope this helps.
Anthony Borla
// ---------------------------------------------------
public class TestPrecedence
{
public static void main(String[] args)
{
int i = 5;
int l, r;
int result = (l = i--) + (r = ++i);
System.out.println("" + i + " " + l + " " + r + " " + result);
// If postfix higher precendence than prefix:
// i-- (5) ...
// i-- (5) + ++i (5) = 10
// If prefix higher precendence than postfix:
// ... ++i (6)
// i-- (6) + ++i (6) = 12
// If postfix same precendence as prefix, then evaluation
// is left to right
// i-- (5) ...
// i-- (5) + ++i (5) = 10
i = 5;
result = i-- + ++i;
System.out.println("" + i + " " + result);
i = 5;
result = (l = --i) + (r = i++);
System.out.println("n" + i + " " + l + " " + r + " " + result);
// If postfix higher precendence than prefix:
// ... i++ (5)
// --i (5) + i++ (5) = 10
// If prefix higher precendence than postfix:
// --i (4) ...
// --i (4) + i++ (4) = 8
// If postfix same precendence as prefix, then evaluation
// is left to right
// --i (4) ...
// --i (4) + i++ (4) = 8
i = 5;
result = --i + i++;
System.out.println("" + i + " " + result);
i = 5; int j = 5;
result = i-- + ++j;
System.out.println("n" + i + " " + j + " " + l + " " + r + " " +
result);
// If postfix higher precendence than prefix:
// i-- (5) ...
// i-- (5) + ++j (6) = 11
// If prefix higher precendence than postfix:
// ... ++j (6)
// i-- (5) + (6) = 11
i = 5; j = 5;
result = --i + j++;
System.out.println("" + i + " " + j + " " + l + " " + r + " " +
result);
// If postfix higher precendence than prefix:
// ... j++ (5)
// --i (4) + j++ (5) = 9
// If prefix higher precendence than postfix:
// --i (4) ...
// --i (4) + j++ (5) = 9
}
}
// ---------------------------------------------------
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Mon Dec 22, 2003 4:27 am Post subject: Re: Precedence of &, |, &&, || operators |
|
|
"lonelyplanet999" <lonelyplanet999 (AT) my-deja (DOT) com> wrote
| Quote: | Tks:
I also found that if I write below lines,
i=1;
i<<=i++;
i will become 2 afterwards. That is only i<<=i where i is still 1
being executed, the ++ operation takes no effect on value of i.
Why will that happens ;(
|
Observe - just as you say:
i = 1;
i <<= i++;
// ... i++ (1)
// (1) <<= (1) ==> 2
System.out.println("" + i);
i = 1; j = 0;
j = i << i++;
// Ditto
System.out.println("" + j + " " + i);
i = 1;
i <<= ++i;
// ... ++i (2)
// (1) <<= (2) ==> 4
System.out.println("" + i);
i = 1; j = 0;
j = i << ++i;
// Ditto
System.out.println("" + j + " " + i);
Why ? The normal order of evaluation is left to right, and any left-hand
value is 'frozen' for purposes of expression evaluation, unaffected by
changes in right hand operand values. The converse, of course, is not true.
A crude way of describing this behaviour [and *not* an attempt to describe
what actually happens at a low level] is:
i <<= ++i;
^ ^
Value Frozen Computed by 'low-level method'
on the Stack implementing the 'increment' operation
Thus, for purposes of expression evaluation, the value of 'i' on the left
hand side is fixed at the value previously asigned to 'i', and is unaffected
by changes occuring on the right hand hand side. Of course, once the
expression has been evaluated, any operands reflect any changes in state
i.e. 'i' is incremented.
The following two examples further illustrate this behaviour:
i = 2;
j = (i = 4) * i;
System.out.println("" + j);
// j = 16
i = 2;
j = i * (i = 4);
System.out.println("" + j);
// j = 8
The assignment to 'i' when used as a left hand operand affects the right
hand operand, *but*, if performed on the right hand operand, leaves the left
alone !
I hope this helps.
Anthony Borla
|
|
| Back to top |
|
 |
lonelyplanet999 Guest
|
Posted: Mon Dec 22, 2003 4:29 am Post subject: Re: Precedence of &, |, &&, || operators |
|
|
Roedy Green <look-at-the-website-for-actual-Roedy (AT) mindprod (DOT) com> wrote
Tks:>
I also found that if I write below lines,
i=1;
i<<=i++;
i will become 2 afterwards. That is only i<<=i where i is still 1
being executed, the ++ operation takes no effect on value of i.
Why will that happens ;(
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Mon Dec 22, 2003 10:15 pm Post subject: Re: Precedence of &, |, &&, || operators |
|
|
On 21 Dec 2003 20:29:45 -0800, [email]lonelyplanet999 (AT) my-deja (DOT) com[/email]
(lonelyplanet999) wrote or quoted :
| Quote: | i<<=i++;
i will become 2 afterwards. That is only i<<=i where i is still 1
being executed, the ++ operation takes no effect on value of i.
|
I'm not the language lawyer type. My first reaction is wish to smack
anyone across the buttocks who writes looney code like that (except of
course to deliberately probe the corners of the language for learning
purposes).
I think it works like this:
The expression is evaluated. Then the postfixes are done. Then the
assignment is done. You could look at the byte code generated. You
could get an oracle to read the JLS.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Alex Hunsley Guest
|
Posted: Tue Dec 23, 2003 12:06 am Post subject: Re: Precedence of &, |, &&, || operators |
|
|
Roedy Green wrote:
| Quote: | On 21 Dec 2003 20:29:45 -0800, [email]lonelyplanet999 (AT) my-deja (DOT) com[/email]
(lonelyplanet999) wrote or quoted :
i<<=i++;
i will become 2 afterwards. That is only i<<=i where i is still 1
being executed, the ++ operation takes no effect on value of i.
I'm not the language lawyer type. My first reaction is wish to smack
anyone across the buttocks who writes looney code like that (except of
course to deliberately probe the corners of the language for learning
purposes).
My thoughts exactly. As a rule, if you're coding in a readable way, you |
shouldn't *have* to know the rules precedence inside out (but knowing
the basics like * binds more tightly than + is necessary)... when you
have complex expressions, either use brackets to make the intention
absolutely clear, or break the expression down into several lines/parts.
This way your code is more readable to yourself and others in the future.
alex
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|