🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#834
From: Tristan Wibberley
Date: Thu, 12 Nov 2026 19:44
53 lines
3654 bytes
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' without increasing
cyclomatic complexity which is very important for safety critical
procedures, as long as you are very regular with how you evaluate
expressions that produce them (I used it in very regular ways having
decided it was okay but I here qualify myself in case some strange
comparisons are not defined to evaluate to either 1 or 0 - "!!" can
often and perhaps always be used).

Both rhs and lhs are evaluated on each evaluation of the whole when '&'
is used, but not when '&&' is used.

I usually add parentheses anyway to these kinds of expressions, or
better yet define a const register local object (which is often easier
to follow in a debugger and to reference an intermediate result in
extrinsic analysis). Helping the reader to know superficially what the
meaning of such expression is lets them put more time elsewhere to
achieve a purpose correctly.


-- 
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: <11d54u2$863c$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>

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: Thu, 12 Nov 2026 19:44:01 +0000
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <11d54u2$863c$1@dont-email.me>
References: <11cj4a0$23796$1@dont-email.me> <11cj8ch$24f93$1@dont-email.me>
 <11cj9he$24mi0$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 12 Nov 2026 19:44:03 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="268396"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19Z/GoSxCDT5KsG3DegG4WBKQectslrZoC+UvAa9jaPow=="; posting-host="61bea730185dc00b7c39d5f27bd5f472"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:SjSHSMMB7j0TtEcNhG5P2T0cMdM= sha256:W9pZf6ZQWQw9ja1zd3lPQ+IH+x0tC+Tw2zkp2JFGClU=
	sha1:Ts+zPkpOqItPW1o8je92YNHBJwE= sha256:6tfYhzdZZY4sZKEF5xcjUNpC/7mD9RZgWOqD5fopzAg=
Content-Language: en-GB
In-Reply-To: <11cj9he$24mi0$1@dont-email.me>