unsigned integer overflow behaviour

Discussion in 'C++' started by bartek, Feb 6, 2004.

  1. bartek

    bartek Guest

    Hello,

    Please help me with the obvious...

    Does the standard say anything about integer overflow? Does it result in
    implementation defined behaviour?

    Would the following code snippet result in '0' being displayed on all
    implementations?

    unsigned x(std::numeric_limits<unsigned>::max());
    std::cout << ++x << std::endl;

    Cheers,
    b
     
    bartek, Feb 6, 2004
    #1
    1. Advertising

  2. bartek

    Ron Natalie Guest

    "bartek" <2.pl> wrote in message news:Xns9487E4C48EE9Cbartekdqwertyuiopo2p@153.19.251.200...

    >
    > Would the following code snippet result in '0' being displayed on all
    > implementations?
    >
    > unsigned x(std::numeric_limits<unsigned>::max());
    > std::cout << ++x << std::endl;
    >

    Yes, It is required that unsigned variables roll over from their max value to zero.

    3.9.1/4
    Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number

    of bits in the value representation of that particular size of integer.
     
    Ron Natalie, Feb 6, 2004
    #2
    1. Advertising

  3. bartek

    Ron Natalie Guest

    "Ron Natalie" <> wrote in message news:4024078d$0$199$...

    > Yes, It is required that unsigned variables roll over from their max value to zero.
    >
    > 3.9.1/4
    > Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number
    >
    > of bits in the value representation of that particular size of integer.
    >

    The thing after modulo is supposed to be 2 raised to the nth power. The superscript got lost in the
    cut and pasting.
     
    Ron Natalie, Feb 6, 2004
    #3
  4. bartek

    bartek Guest

    "Ron Natalie" <> wrote in
    news:40240952$0$170$:

    (...)

    > The thing after modulo is supposed to be 2 raised to the nth power.
    > The superscript got lost in the cut and pasting.


    Thank you. I did extrapolate that though.

    Cheers,
    b
     
    bartek, Feb 6, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond
    Replies:
    8
    Views:
    771
    Raymond
    Aug 20, 2007
  2. Eric Sosman
    Replies:
    1
    Views:
    488
    Eric Sosman
    May 13, 2010
  3. Keith Thompson
    Replies:
    2
    Views:
    736
    Seebs
    May 13, 2010
  4. joe
    Replies:
    23
    Views:
    902
    Seungbeom Kim
    Sep 13, 2010
  5. pozz
    Replies:
    12
    Views:
    743
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page