Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: sum of 64 b...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 20 Topic 26197 of 26972
Post > Topic >>

Re: sum of 64 bits using 32 bits cpu

by Spiros Bousbouras <spibou@[EMAIL PROTECTED] > May 13, 2008 at 11:46 AM

On 13 May, 19:17, sarahh <cohen...@[EMAIL PROTECTED]
> wrote:
> On 13 =D7=9E=D7=90=D7=99, 20:45, Hallvard B Furuseth
<h.b.furus...@[EMAIL PROTECTED]
>
> wrote:
>
> > sarahh writes:
> > > 1.I know that the limit of unsigned integer is ~64000
>
> > ...on a host where unsigned int is 32-bit like on your question,
yes....=

>
> > > what happend in case of overflow it start again from 0.?
>
> > Yes.  With 32-bit unsigned int, 0xffffffffU (all bits =3D 1) + 1U
=3D=3D=
 0.
> > Unsigned arithmetic is defined as modulo-(max value + 1) arithmetic.
>
> > (Strictly speaking this is not what is called overflow in the C
language=

> > - precisely because it is well-defined.  "overflow" is what happens to
> > signed integers, where the result is not defined.)
>
> > > 2.could someone add solution for the question?
>
> > The point of homework is to learn by doing.  But if you need a hint:
> > Think of how you do addition of two-digit numbers by hand, and think
> > of low and high as the two digits of a number.  (In base 0x100000000
> > instead of base 10, but what the hey...)  You need to check somehow
> > whether there was carry from adding the "low" digits.
> >
> Thanks,know I understand it better what about not unsigned, what
> happend if I add to max+1 ?

Undefined behaviour. Hallvard Furuseth has said
so already. Undefined behaviour has as an implication
that you cannot predict the outcome so you must
not allow undefined behaviour to appear in your code.
In other words, when you are dealing with signed
integers you must either know that the values your
programme will be dealing with will be such that no
overflow will occur or you need to check *before*
performing the arithmetic operations whether they
will lead to overflow and if yes take appropriate measures
like emit an error message or something.

Example:

#include <limits.h>
/* ..... */
int a,b,c ;
/* ..... */
/* We want to perform c =3D a+b without risking
 * undefined behaviour.
 */
if ( a >=3D 0 ) {
   if ( b >=3D 0 ) {
      if ( a <=3D INT_MAX - b ) {
         c =3D a+b ;
      } else {
        /* OVERFLOW */
      }
   } else {
      c =3D a+b ;
   }
} else {
   if ( b >=3D 0 ) {
      c =3D a+b ;
   } else {
      if ( a >=3D INT_MIN - b ) {
         c =3D a+b ;
      } else {
        /* OVERFLOW */
      }
   }
}
 




 20 Posts in Topic:
sum of 64 bits using 32 bits cpu
sarahh <cohen200@[EMAI  2008-05-13 10:17:25 
Re: sum of 64 bits using 32 bits cpu
Spiros Bousbouras <spi  2008-05-13 10:40:08 
Re: sum of 64 bits using 32 bits cpu
Hallvard B Furuseth <h  2008-05-13 19:45:22 
Re: sum of 64 bits using 32 bits cpu
sarahh <cohen200@[EMAI  2008-05-13 11:17:36 
Re: sum of 64 bits using 32 bits cpu
sarahh <cohen200@[EMAI  2008-05-13 11:28:04 
Re: sum of 64 bits using 32 bits cpu
roberson@[EMAIL PROTECTED  2008-05-13 18:47:57 
Re: sum of 64 bits using 32 bits cpu
Hallvard B Furuseth <h  2008-05-13 22:27:00 
Re: sum of 64 bits using 32 bits cpu
Spiros Bousbouras <spi  2008-05-13 11:46:39 
Re: sum of 64 bits using 32 bits cpu
Szabolcs Borsanyi <bor  2008-05-14 02:14:55 
Re: sum of 64 bits using 32 bits cpu
sarahh <cohen200@[EMAI  2008-05-14 03:47:02 
Re: sum of 64 bits using 32 bits cpu
Eligiusz Narutowicz<el  2008-05-14 13:07:00 
Re: sum of 64 bits using 32 bits cpu
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-14 05:22:28 
Re: sum of 64 bits using 32 bits cpu
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-14 05:29:16 
Re: sum of 64 bits using 32 bits cpu
Hallvard B Furuseth <h  2008-05-14 14:37:58 
Re: sum of 64 bits using 32 bits cpu
"rio" <a@[EM  2008-05-14 18:57:35 
Re: sum of 64 bits using 32 bits cpu
Bart <bc@[EMAIL PROTEC  2008-05-14 05:35:08 
Re: sum of 64 bits using 32 bits cpu
richard@[EMAIL PROTECTED]  2008-05-14 14:56:41 
Re: sum of 64 bits using 32 bits cpu
Eligiusz Narutowicz<el  2008-05-14 14:51:32 
Re: sum of 64 bits using 32 bits cpu
Szabolcs Borsanyi <bor  2008-05-14 06:10:24 
Re: sum of 64 bits using 32 bits cpu
Spiros Bousbouras <spi  2008-05-14 07:52:33 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:47:51 CDT 2008.