Velocity Reviews > Re: Unsigned Integer Overflow on Multiplication and Division

# Re: Unsigned Integer Overflow on Multiplication and Division

Keith Thompson
Guest
Posts: n/a

 05-13-2010
"Datesfat Chicks" <(E-Mail Removed)> writes:
> What do the standards say about when you multiply two unsigned integers and
> the result is too big?
>
>
> 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.velocityreviews.com/forums/(E-Mail Removed) <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
Guest
Posts: n/a

 05-13-2010
"Datesfat Chicks" <(E-Mail Removed)> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...

[...]
> 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) (E-Mail Removed) <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"

Seebs
Guest
Posts: n/a

 05-13-2010
On 2010-05-13, Datesfat Chicks <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!