short circuit operators

J

Joshua Cranmer

k-e-n said:
3. And I think most important of all: Do Not Mix Boolean Operators and
Bitwise Operators in the Same Expression.

I think that a better guideline would be "Use parenthesis unless the
precedence is obvious". For example, everyone should know what a + b * c
is, or the precedence of a == b || c == d, but a + b | c is not at all
obvious.
 
E

Eric Sosman

Patricia said:
k-e-n wrote:
...

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?

First, I'd fix the "operator || cannot be applied
to int,int" error, which would get me to

if( (intA & mask) != 0 || (intB & mask) != 0 )

.... and then for this particular case I'd rearrange to

if ( ((intA | intB) & mask) != 0 )

.... except that if intB were actually expensiveMethod()
I'd leave well enough alone.
 
P

Patricia Shanahan

Eric said:
First, I'd fix the "operator || cannot be applied
to int,int" error, which would get me to

if( (intA & mask) != 0 || (intB & mask) != 0 )

Sorry, that is what I should have written.

Patricia
 
S

Stefan Ram

Stefanie Ertheld said:
However, the question is probably more "why are non-short circuit
operators evaluated before short circuit operators?!
¯¯¯¯¯¯
Precendence of operators does not imply chronological sequence
of evaluation.

B and BCPL do not have a »||« operator.

In C, »&« is a bit-wise operator for integral types. When

0xffff & x || 0xffff & y

is used, can you guess, which precedence will be needed (wanted)
most of the time?

The precedence was chosen to minimize the need for parentheses.

C++ inherited it from C; Java inherited it from C++.

»Mihai Budiu: Can you tell us about the worse features of C,
from your point of view?

Brian Kernighan: [...] the precedences of some operators
are wrong, but those are trivial things and everybody's
learned to live with them.«

http://www.cs.cmu.edu/~mihaib/kernighan-interview/
 
K

k-e-n

k-e-n wrote:

...


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.
 
A

Arne Vajhøj

k-e-n said:
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
 
L

Lew

Stefan said:
Precendence of operators does not imply chronological sequence
of evaluation.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top