Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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;

 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
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


 
Reply With Quote
 
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


 
Reply With Quote
 
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
modelsim doesn't like my increment w/wraparound Ben Voigt VHDL 8 07-07-2009 06:25 PM
Treating a 1D array as a 2D array with wraparound Dan C Programming 8 11-15-2008 10:59 PM
integer int i; *i++ and ++*i have a different integer value after the increment Robben C++ 14 12-27-2005 03:43 AM
Rollover/wraparound time of time.clock() under win32? Russell Warren Python 3 09-29-2005 04:38 AM
What does the standard say about array access wraparound? David Mathog C Programming 24 06-03-2004 06:59 PM



Advertisments