QUERY: op= rationale

Discussion in 'C Programming' started by Bradford Chamberlain, Jul 20, 2004.

  1. 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
    Bradford Chamberlain, Jul 20, 2004
    #1
    1. Advertising

  2. Bradford Chamberlain

    Michael Mair Guest

    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
    Michael Mair, Jul 20, 2004
    #2
    1. Advertising

  3. Bradford Chamberlain

    Stephen L. Guest

    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
    Stephen L., Jul 21, 2004
    #3
  4. Bradford Chamberlain <> wrote in message news:<>...
    > 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.
    Robert Wessel, Jul 21, 2004
    #4
  5. 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
    Arthur J. O'Dwyer, Jul 21, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harald Kirsch
    Replies:
    0
    Views:
    365
    Harald Kirsch
    Jun 14, 2004
  2. Alexander Grigoriev
    Replies:
    0
    Views:
    371
    Alexander Grigoriev
    Sep 12, 2003
  3. Asfand Yar Qazi
    Replies:
    5
    Views:
    354
    Siemel Naran
    Apr 7, 2004
  4. Christopher Benson-Manica

    gets() rationale

    Christopher Benson-Manica, Dec 2, 2003, in forum: C Programming
    Replies:
    52
    Views:
    1,398
    Mark McIntyre
    Dec 15, 2003
  5. Matt Feinstein

    Rationale for core Python numeric types

    Matt Feinstein, Jun 16, 2004, in forum: Python
    Replies:
    24
    Views:
    823
    Michael Hudson
    Jul 26, 2004
Loading...

Share This Page