473,769 Members | 5,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7484
"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.c om...
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.********@att Abi.com> wrote in message
news:B2KRa.8469 6$Ph3.10486@scc rnsc04...
"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.r esearch.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.r esearch.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
3826
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", myXmlNSMgr).InnerText;
22
53509
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, i.e. I want to process '2' as int: 2, not int: 50 ? Sorry for this newbie question.
5
8041
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 forced to do something like ---snip--- Private Function HexToInteger(ByVal hexValue As String) As Integer
8
2459
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 overflow, but if that isn't guaranteed, how can I portably detect if it would? Alex
7
3149
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
8436
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 = "aabbccdd"; where aa is the LSB and dd is the MSB.
2
1919
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 call and execute the function. All I am able to get back is the Resource id. This is the PHP code: $sqlquery = "dbo.fn_gcc_total_applied '123456'"; $result = odbc_exec($sqlconnect, $sqlquery); if (!$result) { exit("Error in SQL 3"); } Is...
6
2421
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 option, it automatically changes the value to 112. What can i do to correct this to normal decimal value like 112.00,? help me dude!!! thank you,
0
9583
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10039
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6668
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5297
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.