🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#852
From: Tristan Wibberley
Date: Fri, 13 Nov 2026 15:27
112 lines
6486 bytes
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:
>>>> On Thu, 5 Nov 2026 19:52:31 -0500, James Kuyper wrote:
>>>>
>>>>> For historical reasons, bitwise or (|) and bitwise and (&) are
>>>>> exceptions. Unfortunately, correcting that mistake would break
>>>>> backwards compatibility ...
>>>>
>>>> The only real issue is that bitwise ops have lower priority than
>>>> comparisons, when they should be higher. If they were higher, then an
>>>> expression like
>>>>
>>>>      (bitval & mask) == desired_value
>>>
>>>
>>>
>>>> wouldn’t need parentheses.
>>>>
>>>> Realistically, can anyone offer *any* real-world C code that
>>>> uses an expression that would be parenthesized like
>>>>
>>>>      a & (b == c)
>>>
>>> bitwise ops are well used to do logic on 'booleans'
>>
>> oh yeah, for C this is all "int" so I think I would only want to do it
>> with 2s-complement architectures. I think C++ is fine because they're
>> "bool" instead.
>>
>>
> 
> In C, the equality and relational operators evaluate to an "int" value
> of either 0 or 1.  They never result in any other value.

0 has two representations in 1s-complement, all zeros or all ones. I am
worried about that when using bitwise operators to implement logic in
case a comparison yields an all-ones representation of 0. Do comparisons
and ! always produce the all-zeros representation in 1s-complement machines?


> 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". Clocks and
voltage drops are observable. Voltage drops in particular can cause
explosives to land in the wrong place when other systems don't get the
current they need. If you can supply the current without dropping
voltage you still have more heat being produced. It all matters at the
extremes.


> The most sensible way to write your code - IMHO - is to use bitwise
> operators only when you actually mean bitwise operations such as
> masking.  Use logical operators when you mean logical operations - "this
> is true AND that is true" or "this is true OR that is true".  And of
> course, use logical operators if you need to rely on the short-circuit
> evaluation or order of evaluation.

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.

bitwise operators always evaluate both operands but in unspecified order.
logical operators sometimes evaluate only one operand but in specified
order when they evaluate both.

The types of failure that can be caused by that difference can matter.

Using the bitwise operators can cause usage of shared resources to vary
across the domain. Idempotency mitigates this and is advisable widely
anyway, so for techniques to avoid shared resources from existing
anyway. 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"). One can
signal an effect threshold wrapping the logic in a trigger call and so
forth.

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.

It's not perfect to use bitwise, clearly, but the alternative complexity
of a configuration (order vs shortcut) is sometimes an opportune
reduction in complexity of a development process or asset lifetime.


> And don't bother with the "register" keyword.  Optimising compilers have
> ignored that for decades, and C++ removed it in C++17 (while preserving
> it as a keyword for future usage).

It ought to have been kept meaningful in C++ to mask operator& at least.
I recall it was at least as early as C++98 that it was noneffective,
definitely I know it was before C++17 because I never used C++17.


-- 
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: <11d7a99$th7r$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>

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 15:27:37 +0000
Organization: A noiseless patient Spider
Lines: 112
Message-ID: <11d7a99$th7r$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 13 Nov 2026 15:27:38 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="967931"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18QnuSadL9heWo32uQyibWrKW+1GEGxEkl9iTPu+7ekyA=="; posting-host="1a70dd0895ebabe573a20ed7f77e36d0"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:pheClNx2468DJ9p7/w6bWXj+1Mw= sha256:e0n8QP4Fa/divfwB2r04l98GIMpEqEBRz1aVwb6NSUM=
	sha1:0Tes/jFZyrThaLWxAE0CZHztIMw= sha256:c4JUKsd81Kb7Mi0OQUs7nrSj6SrfhkR7ryqpBibMCOw=
Content-Language: en-GB
In-Reply-To: <11d6if2$lqnd$1@dont-email.me>