   How to determine if  my multiplication result overflows
  Hi:One question: How can I determine if the multiplication result c(of int type)of two integers a and b exceeds the max value of int? Any advice will be much appreciatedlike:int a = 999999;int b = 999999;int c = a * b // overflows, but is there any way i can check it?Thanks for help in advance 
  By the way, the language i am using is c++ 6 ;  Convert the answer to binary and use the overflow bit to see weather or not it overflowed.Nice coder ;  if it''s an unsigned 32bit int and you want to see if a*a overflows then check if a >= 2^16. For a*b I''ll have to think about it. ;quote:Original post by Nice Coder  Convert the answer to binary and use the overflow bit to see weather or not it overflowed.Nice coder   Overflow bit!?If you want to know if a value is overflowed, it is usually obvious.  It may be negative, or very small.  It can be hard, though, if there are lots of binary "1"s in the two numbers being multiplied, it can get hard. ;  Or, you could cast your values to long   first (or maybe that''s spelled "long long" on your system... but you ought to have *some* way to get 64-bit types), then multiply, then check if there''s anything in the high order bytes (no overflow = top is all 0''s (positive) or all 1''s (negative).) The multiplication of two 32-bit values *can''t* overflow a 64-bit value ;quote:Original post by PlayGGY  Overflow bit!? It exists in some(all?) x86 cpus, you just need asm to get to it ;  Inline ASM that sets a variable depending on the value of the overflow bit would probably be the best way to do it. ;  If an overflow is generated, the Carry-flag is set. inline bool UMul(size_t a, size_t b, size_t *r) {   __asm { mov    eax,dword ptr [a] mul    dword ptr [b]  mov    ecx,dword ptr [r]  mov    dword ptr [ecx],eax  jc    short j1 mov    al,1  jmp    short j2j1:#ifdef _DEBUG int    3#endif xor    al,alj2:   };}  Cut'n pasted from Michael Howard's "Code Secure"-column.   [edited by - tok_junior on February 3, 2004 12:24:59 PM]Oh, by the way. It returns false if an overflow occured, otherwise true  [edited by - tok_junior on February 3, 2004 12:26:42 PM]