Topic: multiplication overflow?
Author: "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail@moderncppdesign.com>
Date: Tue, 19 Jul 2005 15:26:19 CST Raw View
Hello,
I have a very simple question of which answer I couldn't find in the
standard. What happens if I have an unsigned multiplication that
overflows? Can I count on anything?
unsigned int f1 = UINT_MAX / 2;
unsigned int f1 = UINT_MAX / 3;
unsigned int f3 = f1 * f2;
Can I count on the lower bits of the product sitting in the result?
Andrei
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Wed, 20 Jul 2005 01:53:44 GMT Raw View
Andrei Alexandrescu (See Website For Email) wrote:
> I have a very simple question of which answer I couldn't find in the
> standard. What happens if I have an unsigned multiplication that
> overflows?
3.9.1/4
> Can I count on anything?
>
> unsigned int f1 = UINT_MAX / 2;
> unsigned int f1 = UINT_MAX / 3;
> unsigned int f3 = f1 * f2;
>
> Can I count on the lower bits of the product sitting in the result?
Yep.
V
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: petebecker@acm.org (Pete Becker)
Date: Wed, 20 Jul 2005 01:53:36 GMT Raw View
Andrei Alexandrescu (See Website For Email) wrote:
> Hello,
>
>
> I have a very simple question of which answer I couldn't find in the
> standard. What happens if I have an unsigned multiplication that
> overflows? Can I count on anything?
>
> unsigned int f1 = UINT_MAX / 2;
> unsigned int f1 = UINT_MAX / 3;
> unsigned int f3 = f1 * f2;
>
> Can I count on the lower bits of the product sitting in the result?
>
3.9.1/4: Unsigned integers, declared unsigned, shall obey the laws of
arithmetic modulo 2^n where n is the number of bits in the
value representation of that particular size of integer.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]