Operator precedence in C, particularly in regard to the bitwise operators, can be a bit awkward. Consider an expression like
(val & check_bits) == set_bits
My question is, if this corresponding operator precedence change were to
be made in C, is there any actual real-world code that would break?
But not everyone does write it with the parentheses.
On 06/02/2024 01:24, Lawrence D'Oliveiro wrote:to
My question is, if this corresponding operator precedence change were
be made in C, is there any actual real-world code that would break?
Yes tons of it. Badly written code because in any sensible coding
standard you must have the parentheses.
On Mon, 05 Feb 2024 18:33:56 -0800, Keith Thompson wrote:
But not everyone does write it with the parentheses.
Let us be clear about what we are talking about. You are suggesting that people write an expression like
a «bitop» b «cmp» c
with the expectation that it means
a «bitop» (b «cmp» c)
The question is, who would write something like (with or without
parentheses)
a «bitop» (b «cmp» c)
Given that
is a valid expression that binary ANDs v_a with the truth value of the expression (v_b == v_c), your proposed change in operator precedencev_a & v_b == v_c
would greatly affect the correctness of the expression.
On Tue, 6 Feb 2024 01:37:54 +0000, Malcolm McLean wrote:
On 06/02/2024 01:24, Lawrence D'Oliveiro wrote:to
My question is, if this corresponding operator precedence change were
be made in C, is there any actual real-world code that would break?
Yes tons of it. Badly written code because in any sensible coding
standard you must have the parentheses.
Given that everybody has to write it with the parentheses in existing
code, changing the operator precedence will not affect the correctness of such expressions.
is a valid expression that binary ANDs v_a with the truth value of the expression (v_b == v_c), your proposed change in operator precedence would greatly affect the correctness of the expression.v_a & v_b == v_c
On Tue, 6 Feb 2024 03:01:18 -0000 (UTC), Lew Pitcher wrote:
Given that
is a valid expression that binary ANDs v_a with the truth value of thev_a & v_b == v_c
expression (v_b == v_c), your proposed change in operator precedence
would greatly affect the correctness of the expression.
That’s why I asked for “real-world” examples.
Why would anyone want to
write such an expression with the bitwise operators, instead of using the boolean ones?
Operator precedence in C, particularly in regard to the bitwise operators, can be a bit awkward. Consider an expression like
(val & check_bits) == set_bits
which uses ?check_bits? to mask out the bits to check from ?val?, and then compares the result to ?set_bits?. A more natural precedence order would allow this to be written without the parentheses:
val & check_bits == set_bits
I discovered a few years ago (quite by accident) that Python does make a small change to the operator precedences to allow exactly this sort of
thing.
My question is, if this corresponding operator precedence change were to
be made in C, is there any actual real-world code that would break?
On Mon, 05 Feb 2024 18:33:56 -0800, Keith Thompson wrote:
But not everyone does write it with the parentheses.
Let us be clear about what we are talking about. You are suggesting that people write an expression like
a «bitop» b «cmp» c
with the expectation that it means
a «bitop» (b «cmp» c)
because currently in C, anybody who wants it to mean
(a «bitop» b) «cmp» c
has to write it that way, with explicit parentheses, anyway.
The question is, who would write something like (with or without
parentheses)
a «bitop» (b «cmp» c)
and why? Because a comparison in C only returns either 0 or 1, while bit operators are most useful dealing with multibit masks.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 150:24:47 |
Calls: | 10,383 |
Files: | 14,054 |
Messages: | 6,417,788 |