unsigned integer overflow behaviour

B

bartek

Hello,

Please help me with the obvious...

Does the standard say anything about integer overflow? Does it result in
implementation defined behaviour?

Would the following code snippet result in '0' being displayed on all
implementations?

unsigned x(std::numeric_limits<unsigned>::max());
std::cout << ++x << std::endl;

Cheers,
b
 
R

Ron Natalie

bartek said:
Would the following code snippet result in '0' being displayed on all
implementations?

unsigned x(std::numeric_limits<unsigned>::max());
std::cout << ++x << std::endl;
Yes, It is required that unsigned variables roll over from their max value to zero.

3.9.1/4
Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number

of bits in the value representation of that particular size of integer.
 
R

Ron Natalie

Ron Natalie said:
Yes, It is required that unsigned variables roll over from their max value to zero.

3.9.1/4
Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number

of bits in the value representation of that particular size of integer.
The thing after modulo is supposed to be 2 raised to the nth power. The superscript got lost in the
cut and pasting.
 
B

bartek

(...)
The thing after modulo is supposed to be 2 raised to the nth power.
The superscript got lost in the cut and pasting.

Thank you. I did extrapolate that though.

Cheers,
b
 

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

Similar Threads

Unsigned integer overflow detection 8
optimizing the integer rescaling 9
Overflow on INTEGER value. 24
shift, signed unsigned 5
integer literals 14
Integer overflow... 0
Implicit integer promotion 18
TF-IDF 1

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top