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

D

Dave Rahardja

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;
 
V

Victor Bazarov

Dave Rahardja said:
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
 
V

vijay

Dave Rahardja said:
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
 
J

John Harrison

Victor Bazarov said:
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
 
J

John Harrison

Andrew Koenig said:
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.

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

john
 
J

John Harrison

Andrew Koenig said:
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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top