🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1525

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

#1525
From: Tristan Wibberley
Date: Fri, 13 Nov 2026 22:21
160 lines
8068 bytes
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.


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


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


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


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

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


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

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

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.


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


-- 
Tristan Wibberley

The message body is Copyright (C) 2026 Tristan Wibberley except
citations and quotations noted. All Rights Reserved except that you may,
of course, cite it academically giving credit to me, distribute it
verbatim as part of a usenet system or its archives, and use it to
promote my greatness and general superiority without misrepresentation
of my opinions other than my opinion of my greatness and general
superiority which you _may_ misrepresent. You definitely MAY NOT train
any production AI system with it but you may train experimental AI that
will only be used for evaluation of the AI methods it implements.
Technical Details
Message-ID: <11d82gp$1f70n$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>

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: Tristan Wibberley <tristan.wibberley+netnews2@alumni.manchester.ac.uk>
Newsgroups: comp.lang.c++,comp.lang.c
Subject: Re: does an expression in a logical statement need parenthesis for
 proper evaluation?
Date: Fri, 13 Nov 2026 22:21:12 +0000
Organization: A noiseless patient Spider
Lines: 160
Message-ID: <11d82gp$1f70n$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 13 Nov 2026 22:21:14 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1547287"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19FqdsthcOELp0rt+EafBN0STFUtyJw1LGAAZliaqKSXg=="; posting-host="1a70dd0895ebabe573a20ed7f77e36d0"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:r72NiesKEpUpkwqDAzM5VkRlHKg= sha256:khB4NSkV6mNKOicuIpCtqdbxU+aHI1BaabeXAk4u5rE=
	sha1:zhxYRnPWUB/bAHDUWH45iSnWSu8= sha256:zBDARKyn5ddTeGVY/QtElTMLmZBuavysj4U+bkvJbm0=
Content-Language: en-GB
In-Reply-To: <11d7f1t$1839o$1@dont-email.me>