Re: detecting two's complement arithmetics

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Mycroft Holmes (m.holmes_at_nospam.it)
Date: 11/03/04


Date: Wed, 3 Nov 2004 09:27:31 +0100


>
> But overflow is not allowed!
>
> "If during the evaluation of an expression, the result is not
> mathematically defined or not in the range of representable values for
> its type, the behavior is undefined, unless such an expression is a
> constant expression (5.19), in which case the program is ill-formed."
> (Chapter 5, paragraph 5)
>
> In other words, if it overflows during runtime, you cannot trust the
> result (or anything else). If it overflows at compile time, it is not a
> valid C++ program!
>

unsigned integers must use modular arithmetics, so they cannot overflow, the
simply restart from 0.
so if int_t were unsigned, it would be ok.
if int_t is signed and the machine uses two's complement, ~0 is -1, and
there's no overflow.
otherwise (I understand from your words) the compiler is required to give an
error, isn't it?
If that's true, my test is satisfactory.

-- 
 The set of solutions is never empty.
 Two solutions together form a new problem.
-- Mycroft Holmes


Relevant Pages

  • Re: UINT_MAX+1 = 0 ?
    ... >>> Will it cause any overflow? ... > mechanism for detecting or reporting overflows, ... What are you referring to when you say ther is no mechanism for detecting it ... Compile time reporting is either compile ...
    (microsoft.public.vc.language)
  • Re: Checked and Unchecked operators =?windows-1252?Q?don=92t_s?= =?windows-1252?Q?eem_to_be_
    ... but then you still have an "int" constant as the result of the ... It's because there's no casting that the overflow occurs. ... The destination type is not considered during the actual evaluation of the expression. ... If I understand the above quote correctly, then at compile time it is ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: signed int overflow
    ... that unsigned integers don't overflow at all. ... > but I'd prefer a method I could use within a template. ...
    (comp.lang.cpp)
  • Re: time to get rid of unsigned?
    ... >> unsigned integers. ... It seems quite reasonable to blame the existence of unsigned ... > Overflow pure and simple. ... But because of the mix of signed and unsigned types it does no ...
    (comp.lang.cpp)
  • Re: time to get rid of unsigned?
    ... >> unsigned integers. ... It seems quite reasonable to blame the existence of unsigned ... > Overflow pure and simple. ... But because of the mix of signed and unsigned types it does no ...
    (comp.lang.cpp)