🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++
Article #873

Re: does an expression in a logical statement need parenthesis for proper evaluation?

#873
From: David Brown
Date: Sun, 15 Nov 2026 12:09
288 lines
13522 bytes
On 13/11/2026 23:21, Tristan Wibberley wrote:
> On 13/11/2026 16:49, David Brown wrote:
>> On 13/11/2026 16:27, Tristan Wibberley wrote:
>>> On 13/11/2026 08:41, David Brown wrote:
>>>> On 12/11/2026 23:27, Tristan Wibberley wrote:
>>>>> On 12/11/2026 19:44, Tristan Wibberley wrote:
>>>>>> On 06/11/2026 01:12, Lawrence D’Oliveiro wrote:
> 
>> There is no benefit in using bitwise operators when
>> the logical operators are appropriate - unless you count "confusing
>> readers" as a benefit.
> 
> That's definitely not true, being able to know both lhs and rhs are
> evaluated regardless of the value of the other from just a glance at the
> code is beneficial. Begin able to know that, when they're function
> calls, side-effects of both will occur is beneficial.
> 

As I said, there is no benefit in using bitwise operators when logical 
operators are appropriate.  If you are relying on the evaluation of "B" 
in the expression "A op B", then using logical operators would not be 
appropriate.

But I think it could be considered bad style, and potentially confusing, 
if you wrote "success = foo() & bar();" where these two functions 
returned pass / fail indications and you used this formulation to say 
that both had to be run, with "success" being the conjunction.  It would 
be more idiomatic, I think, to call them separately.  With bitwise and 
here, readers would wonder if you had really meant to use logical and.

The details of the rest of the code, the names of the functions, and so 
on, would of course greatly influence how readers would interpret the 
code.  But my feeling is that when a reader sees at a glance the bitwise 
and operator between two function calls, they would not immediately 
conclude that you used bitwise and so that the rhs was also evaluated - 
they would guess that you made a mistake in the code.


> 
>>>> The only time an optimising
>>>> compiler actually follows the rules about what is and what is not
>>>> evaluated, is when there are side-effects that could potentially lead to
>>>> observable behaviour.
>>>
>>> For sufficiently restrictive definitions of "observable".
>>
>> The C standards define "observable behaviour", and that is the
>> definition I use.
> 
> Noted for this occasion, I will forget that you're using it in future.
> 

I can try and remember to make it clearer in the future.  But since this 
is comp.lang.c and comp.lang.c++, and "observable behaviour" is a very 
specific term from the standards that is critical to understanding the 
semantics of these languages, I think you should get used to the term. 
The word "behaviour" on its own is likely to be used more generally.

> 
>>>> The most sensible way to write your code - IMHO - is to use bitwise
>>>> operators only when you actually mean bitwise operations such as
>>>> masking.
> 
> For this purpose, they actually are logical too, one of the bits
> nominated by the implementation has the required meaning. It is
> commonplace to do this when two or more dimensions are involved, this is
> merely extending the practice down to when just one dimension is involved.
> 

You can certainly use the bitwise operators to have the same effect 
effect as the logical operators (apart from evaluation guarantees), in 
the same way that you can write "x + x + x" instead of "x * 3" - using 
the addition operator to do multiplication.  In general, I don't think 
it is a good idea, even though compilers will often generate identical 
code.  You would even get the same results replacing "&&" with "*", but 
that would be even less of a good idea.

> 
>>> Using shortcutting when you don't require shortcutting has a meaningful
>>> effect on objective measures of the complexity of the system (as does
>>> using an unspecified order with bitwise operators.
>>
>> What meaningful effect?  The evaluation of an expression that does not
>> affect C observable behaviour has /zero/ "meaningful effect" in C.
> 
> Meaningful effect "on objective measures of the complexity of the
> system". shortcutting increases cyclomatic complexity which is often
> considered a cost.
> 

Okay, I can see that :

	// success = foo() && bar()
	bool success = foo();
	if (success) {
		success = bar();
	}

	// success = foo() & bar()
	bool success_foo = foo();
	bool success_bar = bar();
	bool success = success_foo & success_bar;

Yes, the conditional increases the cyclomatic complexity of the code. 
But while cyclomatic complexity can sometimes be a useful and measurable 
metric, I am not at all convinced that it is often a particularly useful 
one except if you need to give an objective reason why a particular bad 
function is "clearly incomprehensible".

> 
>> But do you understand what "evaluates" means here?
> 
> Yes. in great detail.
> 
> 
>>> The types of failure that can be caused by that difference can matter.
>>
>> Do you have an example in mind?  That might make it easier to understand
>> what you are talking about.
> 
> Yes but I'm feeling pretty tired. You will know what kinds of problems
> can occur in a system containing a C program among its constituents
> based on orderings of effects or based on conditional effects.
> 

Fair enough.  It means that I am sticking to "foo() & bar()" examples, 
which might not match what you are thinking about.

Note that when you write "foo() & bar()", where these are both function 
calls, you are guaranteed that either "far()" is evaluated first and 
then "bar()" is evaluated, or vice-versa.  You have no control over 
their order of evaluation.  And if these are not function calls, but are 
other kinds of sub-expressions (or perhaps macros), their evaluation can 
be interleaved.

If you need to control the order of evaluation and side-effects, you 
should do so with separate statements.  Call "foo" first in one 
statement, then call "bar".

>>
>>>
>>> Using the bitwise operators can cause usage of shared resources to vary
>>> across the domain.
>>
>> I have no idea what you mean by this.
> 
> Different cases of a function's inputs could make a resource that both
> operands manipulate change in ways that are described non-uniformly
> across cases, extrapolation from test cases to all other cases to
> predict compliance can be wrong.
> 

I still fail to see a relevancy.  If you can give an example, great.  If 
not, I will leave it.

> 
>>> Idempotency mitigates this and is advisable widely
>>> anyway, so for techniques to avoid shared resources from existing
>>> anyway.
>>
>> I have no idea what you mean by this.  You might be using the word
>> "idempotency" incorrectly.  (An operation "op" is "idempotent" if and
>> only if "x op x == x" for all "x".)
> 
> I understand the term to be used slightly more broadly but only
> slightly. What I had in mind is only irrelevantly different from your
> definition.
> 

Its definition can certainly be broadened beyond just binary operators, 
but I agree that would not be relevant.

> many classes of invalid programs caused by being affected by ordering
> can be made valid by using idempotency in their side-effects.

For example :

	bool initialised;
	void initialise() {
		// Do useful stuff
		initialised = true;
	}

	bool foo() {
		if (!initialised) initialise();
		return do_foo();
	}

	bool bar() {
		if (!initialised) initialise();
		return do_bar();
	}

Then it does not matter which of "foo" and "bar" is called first in 
"foo() & bar()", because they are "idempotent" in ensuring that the 
initialisation is done correctly.  Is this what you are talking about?

The "static initialisation order fiasco" is a real thing, and programs 
have had bugs from getting this kind of thing wrong.  But I can't see 
its relevance in the choice of using bitwise & for logical operations.

> 
> Then I meant "also for techniques..." which are such a big group that
> I'm not going to bother giving examples because this has started to feel
> like a job interview which I do not want.

I am just trying to understand your perspective, that's all.  There is 
no job at the end of it, and no pressure for you to write more than you 
want to.  Discussing how and why people write code in different ways can 
teach us new ways to write code, or give better appreciation for why 
people write or interpret code differently.

> 
> 
>>> The integration solutions for such problems are some of the
>>> intuitive ones relied on when integrating independent active systems
>>> anyway (ie, we already think about systems in terms of "the two effects
>>> happen, make sure your stuff works in case of whichever order").
>>
>> Are you trying to say that since the order of evaluation of the sub-
>> expressions for most operators (including the bitwise ones) is not
>> specified, your code should not rely on any specific ordering?  That is
>> obviously true.
> 
> I'm saying we make systems that do not rely on ordering all the time and
> the management of a development process involving just more of the same
> is easiest.
> 
> 
>>> One can
>>> signal an effect threshold wrapping the logic in a trigger call and so
>>> forth.
>>
>> Again, I have no idea what you are trying to say here.
> 
> One can call a finalisation function after the conditions have been
> evaluated which delimits a segment of program addresses in which some
> signal is permissibly invalid. That can include I/O so that external
> signals are issued.
> 
> 
>>
>>>
>>> Using the logical operators can cause usage of private resources to vary
>>> across the domain. It remains a problem even when other good practices
>>> have been applied.
>>
>> You are going to have to give examples or explain better.
> 
> Different cases of a function's inputs could make a resource that the
> rhs manipulates change in ways that are described non-uniformly across
> cases, extrapolation from test cases to all other cases to predict
> compliance can be wrong.
> 
> Matters of ordering are experienced through systems at all scales and
> are solved with a few practices all over all the systems, in particular,
> systems are very commonly composed of parts that act independently
> except when explicitly excluding each other by signalling protocols (or
> by a scheduler or broker). Matters of shortcutting are different so
> those very common practices don't all apply helpfully, making
> shortcutting, in my mind, a choice to make very cautiously.
> 
> 

When you write code in C or C++, you control the order of observable 
behaviour (as defined by the standards).  You don't control any other 
order, you don't control any timing, and you don't control when the 
compiler-generated code does extra work, or skips work, if the work does 
not influence observable behaviour.

You should not use && if you want the observable behaviour from the rhs 
regardless of the value of the lhs.  That's not special about 
shortcutting logical operators, that's just writing code that does what 
you want it to do.  Similarly, you should not use & for logical 
conjunction if you /do/ want shortcutting semantics.

But IMHO you should not use bitwise & when your code is expressing a 
logical intent - just as you should not use "int" when you mean "bool". 
That is not directly a matter of code correctness, but it is a matter of 
code style and unambiguous understanding from both readers and writers, 
and thus a reduction of the risk of bugs.

It is also very common in coding standards to say that statements should 
do one thing at a time.  Such rules would ban "foo() & bar()" and also 
"foo() && bar()" when both functions may have side-effects.  Follow that 
rule, and there is no risk due to the unspecified order of evaluation, 
nor any need to be concerned about "idempotency".
Technical Details
Message-ID: <11dc7da$2nu91$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11cj4a0$23796$1@dont-email.me> <11cj8ch$24f93$1@dont-email.me> <11cj9he$24mi0$1@dont-email.me> <11d54u2$863c$1@dont-email.me> <11d5eg2$bfhu$1@dont-email.me> <11d6if2$lqnd$1@dont-email.me> <11d7a99$th7r$1@dont-email.me> <11d7f1t$1839o$1@dont-email.me> <11d82gp$1f70n$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c++,comp.lang.c
Subject: Re: does an expression in a logical statement need parenthesis for
 proper evaluation?
Date: Sun, 15 Nov 2026 13:09:14 +0100
Organization: A noiseless patient Spider
Lines: 288
Message-ID: <11dc7da$2nu91$1@dont-email.me>
References: <11cj4a0$23796$1@dont-email.me> <11cj8ch$24f93$1@dont-email.me>
 <11cj9he$24mi0$1@dont-email.me> <11d54u2$863c$1@dont-email.me>
 <11d5eg2$bfhu$1@dont-email.me> <11d6if2$lqnd$1@dont-email.me>
 <11d7a99$th7r$1@dont-email.me> <11d7f1t$1839o$1@dont-email.me>
 <11d82gp$1f70n$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 15 Nov 2026 12:09:16 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2881825"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/+PbwFpIONyTIQiweLZVYlWuT7D1+XNLY="; posting-host="46e21e52d8788b2f7244df84a1ae473d"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:x4iTjivu/+tIMi9aEJb6YaKKsLs= sha256:i+lauuGhHgAV3sfqpI7rbFvCgO73GyMNEst7wb8PBhA=
	sha1:BkL59Z36CRBaZ7oCfrnhbAlM3Cw= sha256:Y5e+HluZ9C26btSBd6DuqTD0BmWTFCuTdxOIH9DytHk=
Content-Language: en-GB
In-Reply-To: <11d82gp$1f70n$1@dont-email.me>