Article View: comp.lang.c
Article #1559Re: does an expression in a logical statement need parenthesis for proper evaluation?
From: Ross Finlayson
Date: Mon, 16 Nov 2026 10:21
Date: Mon, 16 Nov 2026 10:21
352 lines
16520 bytes
16520 bytes
On 11/15/2026 04:09 AM, David Brown wrote:
> 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".
>
One way to read what your saying is "algebraizations" and
"arithmetizations", that's the act of interpreting models of
objects their matters of relation according to the matters of
relation of algebras and arithmetic.
It's totally usual for example with model of "machine" integers
and "machine" arithmetic that being a bounded sort of version
of "natural" integers and "natural" arithmetic, counts or tallies
of things like counters or accumulators according to operations
of arithmetic models of counts of things (which are models of
relations of objects, generally enough, "the things").
Then where for example "== is an operator", it results Boolean
algebras and bit-wise predication Boolean lattices, algebraizations.
It's totally similar with for example operations on formal words
from formal languages like "+" for "concatenation". "Model theory
is as model theory does."
Then AND OR XOT NOT or bit-wise "arithmetic", is sort of about
lattices of algebras, about that algebras aren't necessarily
arithmetic, then that making vector of those is sort of attaining
to "geometrization", or, also algebras of algebras, or sometimes
called "magmas", that "magmas" are a bit looser definition inclusive
of "algebras", which already are pretty loose since for example
"semi-simple" or "non-commutative" groups and the like, divergent from
arithmetic.
So, one can define interpretation of the operations where the
language makes for overloading operators any way you want,
which may make sense when it models natural operations that
happen to stand in as modeling algebras for example arithmetic,
then though that just using arithmetic and machine integers
as the model itself, also requires that it is what it is.
Then "enum" is another usual sort of example, like for "un_set,
un_legal, un_known", reserved values, where reserved values
are another sort of account, like: "Not-A-Number".
Machine integers can't quite achieve all of arithmetic themselves,
while yet there are algebras & magmas of anything.
There's a great book "Data Logic" from the 1970's I found one
day in the university library stacks somewhere in Q section,
been looking for that for a while, and it has lots of great
accounts of data and the logic.
"1's and 0's versus true and false?" Why lose, ....
Technical Details
Message-ID:
<pimdnUKCD4LIRGf3nZ2dnZfqnPWdnZ2d@giganews.com>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mailReferences:
<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> <11dc7da$2nu91$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 16 Nov 2026 10:22:12 +0000 Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation? Newsgroups: comp.lang.c++,comp.lang.c 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> <11dc7da$2nu91$1@dont-email.me> From: Ross Finlayson <ross.a.finlayson@gmail.com> Date: Mon, 16 Nov 2026 02:21:38 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <11dc7da$2nu91$1@dont-email.me> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <pimdnUKCD4LIRGf3nZ2dnZfqnPWdnZ2d@giganews.com> Lines: 352 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-MxzKBIpGAcQ9paEMgqNs5EsW7rWTq+fUW9fvRUiyJaBECfxfpnFsTzwm7BJsB3Bw3Nm3hkLOqyHtjn/!o21XI8k99klrCL8IBwj02Z8hYOQtK91eqD+Uptkknox1mYtUcWonafzV6FEj4yExMzq6w1P8gKGn X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40