# Integer value wraparound -- what does the C++ standard say?

Discussion in 'C++' started by Dave Rahardja, Jul 18, 2003.

1. ### Dave RahardjaGuest

I've tried looking this topic up in the standard manual but came up empty...

1. What is the value of an unsigned integral type after it is decremented
below zero?

2. What is the value of an unsigned integral type after it is incremented past
its maximum value?

3. What is the value of a signed integral type after it is decremented below
its most negative value?

4. What is the value of a signed integral type after it is incremented past
its most positive value?

5. What is the value of x after the following sequence of statements?

unsigned m = 2;
unsigned n = 4;
unsigned x = m - n;

6. How about in the following case?

unsigned a = 2;
unsigned b = -2;
unsigned x = a - b;

Dave Rahardja, Jul 18, 2003

2. ### Victor BazarovGuest

"Dave Rahardja" <> wrote...
> I've tried looking this topic up in the standard manual but came up

empty...

What's the "standard manual"? A Schildt book?

See Standard 3.9.1/4.

> 1. What is the value of an unsigned integral type after it is decremented
> below zero?

Add 2^n where n is the number of bits in the representation, which
makes it (2^n - 1).

> 2. What is the value of an unsigned integral type after it is incremented

past
> its maximum value?

Subtract 2^n. So, it's 0.

> 3. What is the value of a signed integral type after it is decremented

below
> its most negative value?

Undefined.

> 4. What is the value of a signed integral type after it is incremented

past
> its most positive value?

Undefined.

> 5. What is the value of x after the following sequence of statements?
>
> unsigned m = 2;
> unsigned n = 4;
> unsigned x = m - n;

Modulo 2^n, where n == CHAR_BIT*sizeof(unsigned), => (m - n + 2^n).

>
> 6. How about in the following case?
>
> unsigned a = 2;
> unsigned b = -2;

This makes b == 2^n - 2. If 'n' is 16, b == 65534.

> unsigned x = a - b;

Again, modulo 2^n. (2 - 65534 + 65536) => 0.

Victor

Victor Bazarov, Jul 18, 2003

3. ### vijayGuest

"Dave Rahardja" <> wrote in message
news:...
> I've tried looking this topic up in the standard manual but came up

empty...
>
>
> 1. What is the value of an unsigned integral type after it is decremented
> below zero?
>
> 2. What is the value of an unsigned integral type after it is incremented

past
> its maximum value?
>
> 3. What is the value of a signed integral type after it is decremented

below
> its most negative value?
>
> 4. What is the value of a signed integral type after it is incremented

past
> its most positive value?
>
> 5. What is the value of x after the following sequence of statements?
>
> unsigned m = 2;
> unsigned n = 4;
> unsigned x = m - n;
>
> 6. How about in the following case?
>
> unsigned a = 2;
> unsigned b = -2;
> unsigned x = a - b;
>

============================
Consider the numbers are arranged in a circular linked list
So when u do such kind of out of range flat operations, consider the number
are arranged in a circular linked list
A signed chaar can take values from -127 to +127
A unsigned char ca take values from 0 - 255 . SO when u incrments this 255
by 1 value you would 0 ,,,
its somthing like 111111111 + integer 1 = 100000000

regards
Vijay

vijay, Jul 18, 2003
4. ### John HarrisonGuest

"Victor Bazarov" <> wrote in message
news:B2KRa.84696\$Ph3.10486@sccrnsc04...
> "Dave Rahardja" <> wrote...
> > I've tried looking this topic up in the standard manual but came up

> empty...
>
>
> > 3. What is the value of a signed integral type after it is decremented

> below
> > its most negative value?

>
> Undefined.
>
> > 4. What is the value of a signed integral type after it is incremented

> past
> > its most positive value?

>
> Undefined.
>

Its always bugged me that signed overflow results in undefined behaviour.
What's the motivation for it? Supporting non 2-complement architectures
doesn't seem worthwhile but I can't think of any other reason.

john

John Harrison, Jul 18, 2003
5. ### John HarrisonGuest

[OT] Re: Integer value wraparound -- what does the C++ standard say?

"Andrew Koenig" <> wrote in message
news:...
> John> Its always bugged me that signed overflow results in undefined
> John> behaviour. What's the motivation for it?
>
> Detecting integer overflow imposes substantial run-time overhead
> on some processors.
>
> --
> Andrew Koenig,

Really, which ones? Do they have the same problems on unsigned integer
overflow?

john

John Harrison, Jul 18, 2003
6. ### John HarrisonGuest

"Andrew Koenig" <> wrote in message
news:...
> John> Its always bugged me that signed overflow results in undefined
> John> behaviour. What's the motivation for it?
>
> Detecting integer overflow imposes substantial run-time overhead
> on some processors.
>
> --
> Andrew Koenig,

The problem isn't detecting overflow, it what happens when overflow occurs.
All the chips I knew of (which isn't very many) did the obvious 2-complement
wrap around on signed integer overflow. Are you saying that there are some
which don't?

john

John Harrison, Jul 18, 2003