Re: Unsigned Integer Overflow on Multiplication and Division

Discussion in 'C Programming' started by Keith Thompson, May 13, 2010.

  1. "Datesfat Chicks" <> writes:
    > What do the standards say about when you multiply two unsigned integers and
    > the result is too big?
    >
    > Similarly for addition?
    >
    > Is the compiler required to give you the actual result modulo 2^32 or 2^64,
    > or is the behavior pretty much undefined?


    For future reference, the latest post-C99 draft is freely available at
    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>.

    6.2.5p9 says:

    ... A computation involving unsigned operands can never overflow,
    because a result that cannot be represented by the resulting
    unsigned integer type is reduced modulo the number that is one
    greater than the largest value that can be represented by the
    resulting type.

    So yes, for a 32-bit unsigned integer type, the result of any arithmetic
    operation is reduced module 2^32; likewise for 64.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 13, 2010
    #1
    1. Advertising

  2. "Datesfat Chicks" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...

    [...]
    > Ah, OK, and how does one know what standards a compiler has to adhere to?


    Read its documentation and/or check the value of __STDC__ and/or
    __STDC_VERSION__ (look them up in n1256). Note that many compilers
    have different conformance characteristics depending on how you
    invoke them.

    > Are there any standards earlier than C99?


    See question 11.1 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

    The first edition of Kernighan & Ritchie (K&R1) was the de facto
    pre-ANSI standard.

    In addition, there have been three Technical Corrigenda on top of
    C99, all of which are incorporated into n1256. The C99 standard
    officially supersedes the C90 standard, but adoption has not been
    universal. Work is in progress on a new ISO C standard that will
    supersede C99, currently referred to as C201X; the latest draft I'm
    aware of is n1425.pdf, available from the same place as n1256.pdf.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 13, 2010
    #2
    1. Advertising

  3. Keith Thompson

    Seebs Guest

    On 2010-05-13, Datesfat Chicks <> wrote:
    > Ah, OK, and how does one know what standards a compiler has to adhere to?


    It never HAS to adhere to ANY standards.

    But if it doesn't, it doesn't adhere to any standards.

    > Are there any standards earlier than C99?


    Yes, C89.

    In practice, most compilers will be quite good about anything in C89, and
    pretty good about most of C99, with some exceptions. I use C99 features
    like VLAs and compound literals without worrying about it, and it doesn't
    seem to cause me any trouble.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, May 13, 2010
    #3
    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. Replies:
    1
    Views:
    794
    Thomas Matthews
    Jan 21, 2005
  2. Tim923
    Replies:
    4
    Views:
    526
    Tim923
    Apr 8, 2005
  3. William Hughes
    Replies:
    13
    Views:
    1,213
    Ben Bacarisse
    Mar 15, 2010
  4. Eric Sosman
    Replies:
    1
    Views:
    485
    Eric Sosman
    May 13, 2010
  5. Francois Grieu
    Replies:
    0
    Views:
    291
    Francois Grieu
    Jun 8, 2010
Loading...

Share This Page