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


Groups > comp.lang.c > #21985

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:32 -0700
Organization A noiseless patient Spider
Message-ID <kfnaa0dhcye.fsf@x-alumni2.alumni.caltech.edu> (permalink)
References (3 earlier) <jqq3ap$iiu$1@dont-email.me> <jqq4a7$nse$1@dont-email.me> <0.38e73e0042857288d2b5.20120607205812BST.87d35aao8b.fsf@bsb.me.uk> <kfnr4tqgnwz.fsf@x-alumni2.alumni.caltech.edu> <0.a8daec7df0ed7476f02f.20120608120659BST.87d35a9i5o.fsf@bsb.me.uk>

Show all headers | View raw


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

> Tim Rentsch <txr@alumni.caltech.edu> writes:
>
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>
>>> Eric Sosman <esosman@ieee-dot-org.invalid> writes:
> <snip>
>>>>     -1%30u in C *can be* 15.
>>>>
>>>>> -1, 15 and 29; any other possible results for the same expression?
>>>>
>>>>     In C, the possible results are 1, 3, 7, 15.
>>>
>>> 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.
>
> Well that's pleasing to know, although I've never had to
> wrestle with such an implementation.  As far as I can tell, if
> it followed C99 to the letter, the only way to do unsigned
> arithmetic would be to force one or more of the operands to be
> "wider" than unsigned int.  [snip]

Most likely there are no such implementations.  Assuming
however that such hardware platforms still exist, implementors
determined to do a C99 implementation on them would be well
advised to have INT_MAX == UINT_MAX/2 + 1, by making the
high-order bit of a hardware integer value be a 'padding bit'
as far as the C implementation is concerned.  Taking this
approach we could still get a full range of values for
int-only operations (taking advantage of "undefined behavior"
for values that have that bit set), but signed/unsigned
mixtures would not behave pathologically.

(Under the heading of 'note to self':  I really should get in
the habit of putting

    #include <limits.h>
    #if UINT_MAX == INT_MAX
    #error UINT_MAX == INT_MAX -- need I say more
    #endif

in my common header files.)

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