Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #77348

Re: ->=

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: ->=
Date 2015-11-28 13:10 -0800
Organization None to speak of
Message-ID <lnegf9ygne.fsf@kst-u.example.com> (permalink)
References (1 earlier) <kfn7fl3ytq6.fsf@x-alumni2.alumni.caltech.edu> <cf4e364d-de82-449a-bc76-deb580136a79@googlegroups.com> <kfnh9k6xjjl.fsf@x-alumni2.alumni.caltech.edu> <n3cnao$i46$1@speranza.aioe.org> <n3cuam$fbj$1@dont-email.me>

Show all headers | View raw


BartC <bc@freeuk.com> writes:
> On 28/11/2015 17:12, glen herrmannsfeldt wrote:
[...]
>> I have previously noted the absence of ||= and &&=, in some
>> cases using |= or &= in their place.
>
> || and && are also a little funny compared with normal ops.
>
> && for example takes two boolean operands and returns a boolean result.

&& takes two *scalar* operands and yields an *int* result with the value
0 or 1.  One or both operands are compared for inequality to zero.

But yes, the operands and result are what I call "logical boolean",
meaning that we normally care only whether they're zero (treated
as false) or non-zero (treated as true).

> An actual boolean operand would normally be 1 or 0, so that & could be 
> used instead.

An operand of type _Bool could only have the value 0 or 1 (barring
previous undefined behavior).  A scalar operand could have any zero
non-zero value.

> If an operand of && isn't already boolean 1 or 0, for example some 
> arbitrary integer value, then an expression such as (a &&= b) is 
> effectively changing how a is being used.

Presumably if you're evaluating `a &&= b` (or, in actual C, `a = a && b`),
you're already treating a as a "logically boolean" value.

    int cond = isdigit(foo);
    cond &&= isdigit(bar);

> Even if still within the type rules, there's something a bit off about 
> it. You wouldn't normally want to change a count into a boolean for example.

Then you wouldn't normally write `count &&= b`, just as you wouldn't
write `sqrt('x')`.

> The short-circult aspect doesn't help. And it's difficult to visualise 
> how &&= would map into a machine in-place operator as & or + would.

That's no different from the existing semantics of &&.  I'm not
sure what you mean when you say it "doesn't help"; in many ways,
it certainly does.

Mapping &&= to machine instructions is no more difficult than
mapping && to machine instructions -- and I'm content to let the
compiler do that for me.

> If a has a non-zero value, then it will always need to be replaced by 0 
> or 1 (depending on b); never just modified.

Yes.  Which is perfectly normal behavior for values used as conditions
in C.  I admit that C's treatment of zero as false and any non-zero
value as true is peculiar, but we've had several decades to deal with
it.  If you're not used to it by now, ....

Perl's && and || operators yield the value of the last operand
evaluated rather than just a 0/1 or false/true value.  That makes
them more flexible than C's operators -- but of course it's far too
late to change C.  Feel free to consider it for any new languages
you design.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

->= glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-25 14:53 +0000
  Re: ->= Richard Heathfield <rjh@cpax.org.uk> - 2015-11-25 15:12 +0000
    Re: ->= James Kuyper <jameskuyper@verizon.net> - 2015-11-25 11:51 -0500
      Re: ->= "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-25 09:49 -0800
      Re: ->= supercat@casperkitty.com - 2015-11-25 09:55 -0800
    Re: ->= John Bode <jfbode1029@gmail.com> - 2015-11-25 12:24 -0800
      Re: ->= James Kuyper <jameskuyper@verizon.net> - 2015-11-25 15:40 -0500
        Re: ->= John Bode <jfbode1029@gmail.com> - 2015-11-27 17:14 -0800
          Re: ->= James Kuyper <jameskuyper@verizon.net> - 2015-11-27 20:28 -0500
  Re: ->= "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-25 07:35 -0800
  Re: ->= "Charles Richmond" <numerist@aquaporin4.com> - 2015-11-25 15:47 -0600
    Re: ->= James Kuyper <jameskuyper@verizon.net> - 2015-11-25 17:04 -0500
    Re: ->= Nobody <nobody@nowhere.invalid> - 2015-11-26 10:58 +0000
      Re: ->= supercat@casperkitty.com - 2015-11-26 08:55 -0800
  Re: ->= raltbos@xs4all.nl (Richard Bos) - 2015-11-26 00:48 +0000
  Re: ->= Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-27 14:15 -0800
    Re: ->= supercat@casperkitty.com - 2015-11-27 14:26 -0800
      Re: ->= BartC <bc@freeuk.com> - 2015-11-27 23:38 +0000
        Re: ->= Keith Thompson <kst-u@mib.org> - 2015-11-27 19:37 -0800
          Re: ->= Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-28 06:35 -0800
      Re: ->= Stephen Sprunk <stephen@sprunk.org> - 2015-11-27 17:51 -0600
      Re: ->= Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-28 06:53 -0800
        Re: ->= glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-28 17:12 +0000
          Re: ->= Keith Thompson <kst-u@mib.org> - 2015-11-28 10:26 -0800
            Re: ->= glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-28 19:50 +0000
          Re: ->= BartC <bc@freeuk.com> - 2015-11-28 19:14 +0000
            Re: ->= supercat@casperkitty.com - 2015-11-28 11:48 -0800
              Re: ->= glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-29 22:33 +0000
            Re: ->= glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-28 20:18 +0000
              Re: ->= Stephen Sprunk <stephen@sprunk.org> - 2015-11-28 18:15 -0600
                Re: ->= glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-29 22:44 +0000
                Re: ->= Stephen Sprunk <stephen@sprunk.org> - 2015-11-29 18:25 -0600
                Re: ->= raltbos@xs4all.nl (Richard Bos) - 2015-11-30 10:21 +0000
            Re: ->= Keith Thompson <kst-u@mib.org> - 2015-11-28 13:10 -0800
              Re: ->= BartC <bc@freeuk.com> - 2015-11-28 22:12 +0000
                Re: ->= raltbos@xs4all.nl (Richard Bos) - 2015-11-29 11:42 +0000
                Re: ->= BartC <bc@freeuk.com> - 2015-11-29 14:24 +0000
                Re: "->=" Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-29 10:33 -0500
                Re: "->=" BartC <bc@freeuk.com> - 2015-11-29 17:49 +0000
                Re: ->= supercat@casperkitty.com - 2015-11-29 11:50 -0800
                Re: ->= BartC <bc@freeuk.com> - 2015-11-29 22:37 +0000
                Re: ->= Keith Thompson <kst-u@mib.org> - 2015-11-30 00:23 -0800
              Re: ->= glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-29 23:00 +0000
                Re: ->= Keith Thompson <kst-u@mib.org> - 2015-11-30 00:28 -0800
                Re: ->= Rosario19 <Ros@invalid.invalid> - 2015-12-13 16:29 +0100
  Re: ->= Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-28 14:59 -0500

csiph-web