🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1504

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

#1504
From: David Brown
Date: Fri, 13 Nov 2026 08:41
97 lines
6111 bytes
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.  In C++, the 
type of the evaluation is "bool", but the value is identical.  Given 
that there are (in both languages) implicit conversions between bool and 
int and back again that will not affect these values at all, there is no 
practical difference unless you are using the type directly (for sizeof, 
_Generic, type inference, _Alignof).

(Conversion to bool is done by comparing the value to 0.  So any int 
value other than 0 gets converted to "true", while 0 gets converted to 
"false".  Two logical negations - !!x - is sometimes used as a kind of 
"normalisation" operator that in C changes a non-zero scalar value - 
including non-null pointers - to an int of value 1, and changes a zero 
scalar value - including a null pointer - into an int of value 0.  It is 
superfluous if "x" is the result of an equality or relational operator, 
since those already evaluate to 0 or 1.)

When applied to values that are either 0 or 1, the bitwise operators & 
and | will give the same results as the logical operators && and ||.

The bitwise operators will always result in code that acts as if it 
evaluates the right-hand side of the expression.  The logical operators 
result in code that acts as if it evaluates the right-hand side if and 
only if the evaluation of the left-hand side does not determine the 
final evaluation.

Note the "acts as if".  If you write "if ((x > 2) && (x < 10))", the 
generated code will almost certainly be the same as if you used bitwise 
&, and both sub-expressions will be evaluated - because that is more 
efficient than using a conditional branch.  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.  Outside of that, the compiler will generate what 
it thinks is the most efficient result.

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.


In terms of style, for complex expressions you should always use 
parentheses and/or split up the expression into parts assigned to local 
variables, according to what you think gives the clearest and most 
maintainable code that fits with any coding style requirements you have 
for the project, and perhaps with consideration for ease of debugging 
tools if that is relevant.  Optimising compilers are likely to generate 
identical code regardless of how you split things up.  There is nothing 
special about logical or bitwise operators here.

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


None of this has any connection to the representation of signed 
integers, or whether that is two's complement or otherwise.  No known 
modern C compilers (i.e., anything beyond perhaps C99) use anything 
other than two's complement - no known C++ compilers have ever supported 
anything else.  Signed integer representation is always two's complement 
since C23 and C++20.

Technical Details
Message-ID: <11d6if2$lqnd$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!news-ams.pugleaf.net!news.tcpreset.net!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!news.usenet.ovh!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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!news-ams.pugleaf.net!news.tcpreset.net!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!news.usenet.ovh!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 09:41:06 +0100
Organization: A noiseless patient Spider
Lines: 97
Message-ID: <11d6if2$lqnd$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 13 Nov 2026 08:41:07 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="715501"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+EP/Jw2pK33JrnEdHQxcPhBgnZEoAJG0c="; posting-host="836c897653923955ff8b6515cf6f95e1"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:KOrUNQMVKTgLtkC+h4bqoDTfx+Y= sha256:7npJbELvB4XSz/L4vzKzhTNsvgojpzb4XwOhGU1A3ow=
	sha1:AavqkSFrTvD4s998KX2MrCghGGU= sha256:VSwaW4tX1m1hjbtVzoKofVsT6vgEljkBnbHtxiK+FcY=
Content-Language: en-GB, nb-NO
In-Reply-To: <11d5eg2$bfhu$1@dont-email.me>