Velocity Reviews > C++ > Integer value wraparound -- what does the C++ standard say?

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

Dave Rahardja
Guest
Posts: n/a

 07-18-2003
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;

Victor Bazarov
Guest
Posts: n/a

 07-18-2003
"Dave Rahardja" <(E-Mail Removed)> 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

vijay
Guest
Posts: n/a

 07-18-2003

"Dave Rahardja" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

John Harrison
Guest
Posts: n/a

 07-18-2003

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:B2KRa.84696\$Ph3.10486@sccrnsc04...
> "Dave Rahardja" <(E-Mail Removed)> 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
Guest
Posts: n/a

 07-18-2003

"Andrew Koenig" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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, http://www.velocityreviews.com/forums/(E-Mail Removed)

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

john

John Harrison
Guest
Posts: n/a

 07-18-2003

"Andrew Koenig" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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, (E-Mail Removed)

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