short circuit operators  
Author Message
k-e-n





PostPosted: 2008-2-26 8:43:00 Top

java-programmer, short circuit operators On Feb 25, 5:19 pm, Patricia Shanahan <email***@***.com> wrote:
> k-e-n wrote:
>
> ...
>
> > Also as I mention in the list below it is simply not good practice to
> > mix Bitwise and Boolean operators in the same expression, just because
> > you can, does not mean you should.
> ...
> > 3. And I think most important of all: Do Not Mix Boolean Operators and
> > Bitwise Operators in the Same Expression.
>
> Note that "|" and "&" with boolean operands, such as true and false, are
> not bitwise operations. They are boolean operations without
> short-circuiting, so that the right hand side is evaluated regardless of
> the left hand side value. They are only bitwise operators if applied to
> integer operands.
>
> What is wrong with mixing bitwise and boolean operators in the same
> expression? Suppose I want to do something if a particular bit is on in
> either of two ints:
>
> if( (intA & mask) || (intB & mask) )
>
> seems reasonable to me, but it definitely mixes bitwise and boolean
> operations. How would you do it?
>
> Patricia

I would do it as you have done and as Joshua Cranmer suggests, use
parenthesis to make the precedence and more specifically your intent
clear.
To a programmer the parenthesis are sufficient advice, to someone who
seems not to understand precedence or perhaps more specifically wants
to know why the rule(s)
exists and what is the meta-rule behind that, I felt something
stronger was required.

Did you have any problem with any other parts of my comment? I have no
wish to cause offense or create any controversy.
 
k-e-n





PostPosted: 2008-2-26 8:43:00 Top

java-programmer >> short circuit operators On Feb 25, 5:19 pm, Patricia Shanahan <email***@***.com> wrote:
> k-e-n wrote:
>
> ...
>
> > Also as I mention in the list below it is simply not good practice to
> > mix Bitwise and Boolean operators in the same expression, just because
> > you can, does not mean you should.
> ...
> > 3. And I think most important of all: Do Not Mix Boolean Operators and
> > Bitwise Operators in the Same Expression.
>
> Note that "|" and "&" with boolean operands, such as true and false, are
> not bitwise operations. They are boolean operations without
> short-circuiting, so that the right hand side is evaluated regardless of
> the left hand side value. They are only bitwise operators if applied to
> integer operands.
>
> What is wrong with mixing bitwise and boolean operators in the same
> expression? Suppose I want to do something if a particular bit is on in
> either of two ints:
>
> if( (intA & mask) || (intB & mask) )
>
> seems reasonable to me, but it definitely mixes bitwise and boolean
> operations. How would you do it?
>
> Patricia

I would do it as you have done and as Joshua Cranmer suggests, use
parenthesis to make the precedence and more specifically your intent
clear.
To a programmer the parenthesis are sufficient advice, to someone who
seems not to understand precedence or perhaps more specifically wants
to know why the rule(s)
exists and what is the meta-rule behind that, I felt something
stronger was required.

Did you have any problem with any other parts of my comment? I have no
wish to cause offense or create any controversy.
 
Arne Vajh鴍





PostPosted: 2008-2-26 11:19:00 Top

java-programmer >> short circuit operators k-e-n wrote:
> Also as I mention in the list below it is simply not good practice to
> mix Bitwise and Boolean operators in the same expression, just because
> you can, does not mean you should.

The test should reflect the functionality you want. If the functionality
requires mixing shortcircuit and non-shortcircuit operators, then there
is not much to do about it.

Arne
 
 
Lew





PostPosted: 2008-2-26 12:01:00 Top

java-programmer >> short circuit operators Stefan Ram wrote:
> Precendence of operators does not imply chronological sequence
> of evaluation.

<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7.3>
> Java programming language implementations must respect the order of evaluation
> as indicated explicitly by parentheses and implicitly by operator precedence.

It's one of the ways in which Java explicitly went in a different direction
from its forebears, up there with fixing the domains of primitive types.

--
Lew