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


Groups > comp.lang.c > #21980

Re: Signed mod unsigned

From Tim Rentsch <txr@alumni.caltech.edu>
Newsgroups comp.lang.c
Subject Re: Signed mod unsigned
Date 2012-06-08 11:12 -0700
Organization A noiseless patient Spider
Message-ID <kfnehpphdvd.fsf@x-alumni2.alumni.caltech.edu> (permalink)
References (4 earlier) <jqq4a7$nse$1@dont-email.me> <0.38e73e0042857288d2b5.20120607205812BST.87d35aao8b.fsf@bsb.me.uk> <kfnr4tqgnwz.fsf@x-alumni2.alumni.caltech.edu> <jqsgu1$4so$1@news.albasani.net> <0.98a376f61e7465ca9f4c.20120608123059BST.877gvi9h1o.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Johannes Bauer <dfnsonfsduifb@gmx.de> writes:
>
>> On 08.06.2012 11:20, Tim Rentsch wrote:
>>
>>>> I think -1 is also possible on those awkward systems where UINT_MAX ==
>>>> INT_MAX.
>>> 
>>> Sort of.  This curious behavior was not present in C90 or
>>> in the original C99.  It was added via a TC (it appears
>>> in N1124) as a result of Defect Report 230.  However,
>>> subsequently it was acknowledged that the strange effect
>>> on the promotion rules was an unintended consequence of a
>>> poor wording choice to address the DR.  This has since
>>> been corrected in C11 -- under C11 unsigned int is never
>>> 'promoted' to int even if UINT_MAX == INT_MAX.  Hence the
>>> expression -1%30u can never be -1 (or any negative number)
>>> under the current standard.
>>
>> Wow -- I really don't feel bad about tripping over this now. It *is*
>> indeed confusion and without a *lot* of insight into the standard it is
>> quite hard to tell what the expression will evaluate to on different
>> platforms.
>
> I don't want to make you feel bad again, but this last case is one of
> the oddest corners of C and can be quite safely ignored by almost
> everyone (indeed by everyone, soon, if C11 gets a hold).
>
> The main fact, that mixed int and unsigned int arithmetic is done by
> converting the int operand to unsigned int, is commonplace and needs to
> be kept in mind for those times when it will matter.  The fact that it's
> happening is often masked by implementations that use 2's complement and
> define the unsigned to int conversion as simply "stuffing the bits back
> in there".  However
>
>   int x;
>   /* ... */
>   x = x + 1u;
>
> can raise a signal when x is, say, -1 on systems that define the
> conversion of out-of-range values to int as doing so.  [snip]

Forgive me if this is a stupid question -- can you explain how
starting with x == -1 can give rise to anything other than x == 0
after the assignment?  To me the behavior here looks well-defined.

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


Thread

Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-06 15:55 +0200
  Re: Signed mod unsigned James Kuyper <jameskuyper@verizon.net> - 2012-06-06 10:41 -0400
    Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-07 12:12 +0200
      Re: Signed mod unsigned James Kuyper <jameskuyper@verizon.net> - 2012-06-07 06:44 -0400
        Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-07 13:31 +0200
  Re: Signed mod unsigned gwowen <gwowen@gmail.com> - 2012-06-06 07:53 -0700
    Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-07 12:15 +0200
      Re: Signed mod unsigned "BartC" <bc@freeuk.com> - 2012-06-07 12:29 +0100
        Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-07 13:43 +0200
        Re: Signed mod unsigned Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-07 07:46 -0400
          Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-07 20:58 +0100
            Re: Signed mod unsigned Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-07 19:58 -0400
            Re: Signed mod unsigned Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 02:20 -0700
              Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-08 11:33 +0200
                Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-08 12:30 +0100
                Re: Signed mod unsigned Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 11:12 -0700
                Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-08 20:37 +0100
                Re: Signed mod unsigned Keith Thompson <kst-u@mib.org> - 2012-06-08 13:44 -0700
                Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-08 22:22 +0100
                Re: Signed mod unsigned Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-10 10:56 +0200
                Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-10 14:40 +0100
                Re: Signed mod unsigned Keith Thompson <kst-u@mib.org> - 2012-06-11 15:49 -0700
                Re: Signed mod unsigned Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-24 09:32 -0700
                Re: Signed mod unsigned Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 11:05 -0700
              Re: Signed mod unsigned Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-08 12:06 +0100
                Re: Signed mod unsigned Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 11:32 -0700
  Re: Signed mod unsigned Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-06-06 22:15 +0000
    Re: Signed mod unsigned James Kuyper <jameskuyper@verizon.net> - 2012-06-06 18:29 -0400
      Re: Signed mod unsigned Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-06-07 20:44 +0000
        Re: Signed mod unsigned James Kuyper <jameskuyper@verizon.net> - 2012-06-07 17:08 -0400

csiph-web