473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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;

Jul 19 '05 #1
5 7447
"Dave Rahardja" <as*@me.com> 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
Jul 19 '05 #2

"Dave Rahardja" <as*@me.com> wrote in message
news:6p********************************@4ax.com...
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
Jul 19 '05 #3

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:B2KRa.84696$Ph3.10486@sccrnsc04...
"Dave Rahardja" <as*@me.com> 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
Jul 19 '05 #4

"Andrew Koenig" <ar*@acm.org> wrote in message
news:yu**************@tinker.research.att.com...
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, ar*@acm.org


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

john
Jul 19 '05 #5

"Andrew Koenig" <ar*@acm.org> wrote in message
news:yu**************@tinker.research.att.com...
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, ar*@acm.org


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
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sashi | last post by:
Two questions: (1) I can pull the text of an XML element as a string just fine using code as such: strSomeString = myXmlDoc.SelectSingleNode("/Element1/Element2/Element3",...
22
by: hantie | last post by:
In my program, for reducing the complexity, the integer was stored as char, so 2 is stored as '2', which is decimal: 50 Is is possible for me to obtain the integer value of '2' in the program,...
5
by: Joergen Bech | last post by:
Basically, I want to convert hex values in the range "00000000" to "FFFFFFFF" to a signed, 32-bit Integer value. In VB6, I could just write lngValue = Val(hexstring$). In VB.Net, I seem to be...
8
by: Alex Fraser | last post by:
Can negating a non-negative signed integer value ever overflow? Put another way, can it be true that (mathematically) INT_MIN > -INT_MAX, LONG_MIN > -LONG_MAX etc? I know that typically it can't...
7
by: fool | last post by:
Dear group, Extract the integer value present in a given string. So I tried the following: int main(void) { int val; char *data; data = malloc(sizeof *data); if(data)
8
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer =...
2
by: fredb | last post by:
I am trying to retrieve an integer value that is returned from an MS SQL function that our DBA wrote and is saved in a table in the database. Using the ODBC_EXEC command I am able to successfully...
6
by: phpmagesh | last post by:
Hai friends, {$PRODUCTS_PRICE} This is my variable i dont know why they used { } for that. in that product_price i m storing a integer value like 112.00 etc.., when i change to another radio...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.