QUERY: op= rationale

  • Thread starter Bradford Chamberlain
  • Start date
B

Bradford Chamberlain

I've been curious for awhile about why C supports some of its binary
operators in an op= format, but not others. For example, why are &=
and |= supported by C, but not &&= and ||=?

For awhile I was guessing that it was because the designers wanted to
avoid 3-character operators, but then I remembered <<= and >>=.

If anyone has any insight into the rationale for this, please let me
know. My sense has been that such operators would occasionally be
useful, and I've considered adding them to my own C-like languages,
but want to be sure I'm not overlooking something subtle.

Thanks very much,
-Brad
 
M

Michael Mair

Hi Brad,


there is no definite answer from my side, just stating the obvious -
sometimes that can be a help, too.

I've been curious for awhile about why C supports some of its binary
operators in an op= format, but not others. For example, why are &=
and |= supported by C, but not &&= and ||=?

If anyone has any insight into the rationale for this, please let me
know. My sense has been that such operators would occasionally be
useful, and I've considered adding them to my own C-like languages,
but want to be sure I'm not overlooking something subtle.

The "supported" operators are the binary operators for arithmetic
and bitwise operations. IMO, reasons are

- the behaviour and types of operands and result is clear or easy to
define
- these operations are used rather often, so a shortcut makes sense
- for early none-too-intelligent compilers, this was a help to generate
assembler code which was faster if there were corresponding
operations (e.g. on a Motorola 68xx I've been playing with,
adding two values and storing the result in a different value
took one cycle longer than adding one value to another)
- in the same vein: this is AFAIK not true for logical operations and
the like

Now, introducing &&=, ||=, ===, ->=, .=, ... in general may be
a nice idea if you need it a lot; even ?=: is an option.

For the logical AND and OR as well as for ==,>=,<=, I think that
the readability of your code might suffer, apart from the
higher probability of errors by typing not enough or too many
&,|,=.
The ->= is also prone to the latter problem, if you manage to
forget the >.
For <=,>= there are <== and >== but what is there for <,> ?

Maybe someone else can contribute a little bit more.


So, the only "real" downsides are that you have to teach it to the
compiler and that you'll be one of a very small group to use
the resulting language in full :)


Cheers,
Michael
 
S

Stephen L.

Bradford said:
I've been curious for awhile about why C supports some of its binary
operators in an op= format, but not others. For example, why are &=
and |= supported by C, but not &&= and ||=?

For awhile I was guessing that it was because the designers wanted to
avoid 3-character operators, but then I remembered <<= and >>=.

If anyone has any insight into the rationale for this, please let me
know. My sense has been that such operators would occasionally be
useful, and I've considered adding them to my own C-like languages,
but want to be sure I'm not overlooking something subtle.

Thanks very much,
-Brad

Think 'bout the long version of what you're suggesting ->

a &&= b;

would be

a = a && b;

Would you use such an expression in a _real_ program
(hint: `&&' and `||' aren't binary operators)?


-
Stephen
 
R

Robert Wessel

Bradford Chamberlain said:
I've been curious for awhile about why C supports some of its binary
operators in an op= format, but not others. For example, why are &=
and |= supported by C, but not &&= and ||=?

For awhile I was guessing that it was because the designers wanted to
avoid 3-character operators, but then I remembered <<= and >>=.

If anyone has any insight into the rationale for this, please let me
know. My sense has been that such operators would occasionally be
useful, and I've considered adding them to my own C-like languages,
but want to be sure I'm not overlooking something subtle.


I suspect it's mainly a lack of general utility - yes it might be
useful on occasion, but not very often. Second, how exactly do you
intend to define the short-circuit semantics for "||="? Neither
option is very attractive. Then there's the workaround, which is
trivial, so why bother.
 
A

Arthur J. O'Dwyer

Think 'bout the long version of what you're suggesting ->

a &&= b;

would be

a = a && b;

Would you use such an expression in a _real_ program
(hint: `&&' and `||' aren't binary operators)?

Double hint: they sure as heck ain't unary! The word you're
aiming for is "bitwise," and it's completely irrelevant.

#include <stdbool.h>

bool rabbitsFromAllThreeHats(void)
{
bool rc = true;
rc &&= rabbitFromHatA();
rc &&= rabbitFromHatB();
rc &&= rabbitFromHatC();
return rc;
}

-Arthur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top