Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > QUERY: op= rationale

Reply
Thread Tools

QUERY: op= rationale

 
 
Bradford Chamberlain
Guest
Posts: n/a
 
      07-20-2004

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


 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      07-20-2004
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

 
Reply With Quote
 
 
 
 
Stephen L.
Guest
Posts: n/a
 
      07-21-2004
Bradford Chamberlain wrote:
>
> 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
 
Reply With Quote
 
Robert Wessel
Guest
Posts: n/a
 
      07-21-2004
Bradford Chamberlain <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      07-21-2004

On Tue, 20 Jul 2004, Stephen L. wrote:
>
> Bradford Chamberlain wrote:
> >
> > 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 ||=?

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about rationale for java.nio.Buffer design. Harald Kirsch Java 0 06-14-2004 12:15 PM
Re: The Sigma-Foveon pixel rationale Dave Martindale Digital Photography 42 04-06-2004 10:11 PM
Re: The Sigma-Foveon pixel rationale Dave Martindale Digital Photography 2 04-02-2004 08:49 PM
Re: The Sigma-Foveon pixel rationale Dave Martindale Digital Photography 2 04-01-2004 05:01 PM
Re: The Sigma-Foveon pixel rationale David J. Littleboy Digital Photography 2 04-01-2004 08:11 AM



Advertisments