Article View: comp.lang.c++
Article #853Re: does an expression in a logical statement need parenthesis for proper evaluation?
From: David Brown
Date: Fri, 13 Nov 2026 16:49
Date: Fri, 13 Nov 2026 16:49
224 lines
11085 bytes
11085 bytes
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:
>>>>> 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?
>
In C implementations using ones' complement, the two representations of
integer value 0 (if all one bits is considered a value rather than a
trap representation) always compare equal. There are only a few
operations that can potentially produce a negative zero, and the
relational operators are not amongst those. Bitwise operators /can/
produce a negative zero, but you would have to start off with negative
numbers to get them - an expression mixing relational operators and
bitwise operators (other than bitwise negation) cannot produce anything
other than 0 or 1.
And there are no C implementations that are not two's complement, other
than a few ancient compilers for museum mainframes. You can be entirely
confident that your code will never be used on them.
But as I said before, if you are writing a logical expression, use the
logical operators. There is no benefit in using bitwise operators when
the logical operators are appropriate - unless you count "confusing
readers" as a benefit.
>
>> 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.
> 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.
Lots of things matter in real-world programming other than "observable
behaviour" in C code. But it is only the C standards definition of
"observable behaviour" that matters for the semantics of the C code, or
how the compiler looks at it. When choosing whether or not to evaluate
the right-hand side of an operator expression, the compiler cares about
what the C standards say - not any non-C effects. And if the compiler
can see that it has a choice in how to generate the code, it will pick
based on flags for speed optimisation, size optimisation, debugging, etc.
>
>
>> 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.
What meaningful effect? The evaluation of an expression that does not
affect C observable behaviour has /zero/ "meaningful effect" in C.
First understand the C semantics - what the C code means, and what the
"as if" rule means. That's how you can understand what "correctness"
means in C programming. C has no concept of speed, or code size, or
suchlike. Only once you have got that under control can you start
looking at non-C attributes, like code speed. The speed of code can of
course be a vital element in the usefulness of a program for a given
task, but it is pointless to worry about speed until you know what is
correct or not. Fast but wrong is no use to anyone.
>
> bitwise operators always evaluate both operands but in unspecified order.
Correct.
> logical operators sometimes evaluate only one operand but in specified
> order when they evaluate both.
Correct.
But do you understand what "evaluates" means here?
Look at the generated code for simple test code :
<https://godbolt.org/z/9Wxsheqhd>
int test1(int x) {
return (x > 2) && (x < 10);
}
int test2(int x) {
return (x > 2) & (x < 10);
}
It test1, the semantics of C require that the generated code acts /as
if/ the second test "x < 10" is evaluated if and only if the first test
"x > 2" passes. And yet the compiler generates identical code for both
test1 and test2. It does this because evaluating "x < 10" has no
side-effects, and the most efficient results are produced by doing that
calculation every time.
>
> 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.
>
> Using the bitwise operators can cause usage of shared resources to vary
> across the domain.
I have no idea what you mean by this.
> 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".)
> 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.
> 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.
>
> 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. The only
thing I can think of is for cryptography applications where you want to
be sure that timings, processor power usage, and other related factors
are independent of the values being used. To give a simplistic example,
you don't want to have a password check algorithm that returns a failure
as soon as it hits a mismatched character - then an attacker could use
that information to know which character was incorrect. In general, C
has no way to express things like constant timings - cryptographic
algorithms that need that cannot be written in standard C.
>
> 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.
>
Prior to C++17, the "register" keyword had the same meaning in C++ as it
does in C. It is a storage-class specified. The only semantic effect
of declaring an object with "register" is that it is a constraint
violation to apply the & operator to it - i.e., you cannot take its
address. It may be seen as a hint to the compiler that the variable
should be optimised for speed - but optimising compilers have ignored
that for decades, since they optimise /everything/. It may have some
small influence on some compilers if you do not use optimisation, but it
is meaningless to be concerned about the speed of code when not optimising.
Technical Details
Message-ID:
<11d7f1t$1839o$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-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>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: Fri, 13 Nov 2026 17:49:00 +0100 Organization: A noiseless patient Spider Lines: 224 Message-ID: <11d7f1t$1839o$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 13 Nov 2026 16:49:02 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1314104"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/U/NS94pCY25gkyeftKhVMzcKAogZn3EI="; posting-host="836c897653923955ff8b6515cf6f95e1" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:Bjn3wsajDbGsQ4jM44YbqNn5cQU= sha256:Tiy/6SEHOfpwl7+leP+RgN546duHDve7wuxuKY1sgH0= sha1:jGouX3v3VqQgP+B0SLVDThUVYJo= sha256:mvXUlSosAtvPRlwWJkgifViIkNd0Hdf45tw5yIXQ2sY= In-Reply-To: <11d7a99$th7r$1@dont-email.me> Content-Language: en-GB, nb-NO