QUERY: op= rationale

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bradford Chamberlain

    #1

    QUERY: op= rationale


    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


  • Michael Mair

    #2
    Re: QUERY: op= rationale

    Hi Brad,


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

    [color=blue]
    > 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.[/color]

    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

    Comment

    • Stephen L.

      #3
      Re: QUERY: op= rationale

      Bradford Chamberlain wrote:[color=blue]
      >
      > 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[/color]

      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

      Comment

      • Robert Wessel

        #4
        Re: QUERY: op= rationale

        Bradford Chamberlain <bradc@lotus.wc .cray.com> wrote in message news:<qkmllher0 1p.fsf@lotus.wc .cray.com>...[color=blue]
        > 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.[/color]


        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.

        Comment

        • Arthur J. O'Dwyer

          #5
          Re: QUERY: op= rationale


          On Tue, 20 Jul 2004, Stephen L. wrote:[color=blue]
          >
          > Bradford Chamberlain wrote:[color=green]
          > >
          > > 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 ||=?[/color]
          >
          > 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)?[/color]

          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 rabbitsFromAllT hreeHats(void)
          {
          bool rc = true;
          rc &&= rabbitFromHatA( );
          rc &&= rabbitFromHatB( );
          rc &&= rabbitFromHatC( );
          return rc;
          }

          -Arthur

          Comment

          Working...