## Why does this not cause an overflow?

### Why does this not cause an overflow?

So I just started C++ and hit the point reading about overflows. I was curious of how overflows worked, and started messing wih them. I made the code below and I was curious of why it does not produce an overflow.
Below we make x = to 65535, in the first expression, we add 1 to x giving us 65536 which equals 1 0000 0000 0000 0000 in binary, making it wrap around and cause a overflow and now x will = zero. In the second expression we take the same variable x which now equals 65536 but prints as if x is = to 0, and then we subtract the 65535 from it and add it right back. x then prints out 65536.
I am a bit newer to this, could anyone explain why the second expression does not give an overflow?

Code: Select all
`#include <iostream>int main(){    using namespace std;    unsigned short x = 65535; // largest 2-byte unsigned value possible    cout << "x was: " << x << endl;    x = x + 1;        //We add 1 to 65535 which gives an overflow.    cout << "x is now: " << x << endl;    //Subctract 66535 from x and then add it back to x    cout << "Bypass the overflow: "; x = x - 65535; cout << x + 65535; cout << endl;}`

For those that know
K: 0x2CD8D4F9

-Ninjex-
Moderator

Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)

### Re: Why does this not cause an overflow?

Are you on a 64bit machine?

Note: it may not even be dependent on your data model and depends mostly on the implementation (compiler).
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.

fashizzlepop
Developer

Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)

### Re: Why does this not cause an overflow?

fashizzlepop wrote:Are you on a 64bit machine?

Note: it may not even be dependent on your data model and depends mostly on the implementation (compiler).

32bit System

Code: Select all
`\$ uname -aLinux Ninjex 3.2.0-35-generic-pae #55-Ubuntu SMP Wed Dec 5 18:04:39 UTC 2012 i686 athlon i386 GNU/Linux`

g++ is what I use to compile I used the following syntax from g++

Code: Select all
`g++ test.cpp -o test`

Code: Select all
`\$ ./test x was: 65535x is now: 0Bypass the overflow: 65536`
Last edited by -Ninjex- on Sat Jan 19, 2013 9:29 pm, edited 1 time in total.

For those that know
K: 0x2CD8D4F9

-Ninjex-
Moderator

Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)

### Re: Why does this not cause an overflow?

So... It worked?
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.

fashizzlepop
Developer

Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)

### Re: Why does this not cause an overflow?

fashizzlepop wrote:So... It worked?

Yes, I do not understand how dropping 65535 and adding it back to 65536 makes it print out 65536 after it previously printed 0

For instance:

x = 65535
//printed as 65535

x + 1 = 65536
//printed as 0

x - 65535; x + 65535 = 65536
//printed as 65536
Last edited by -Ninjex- on Sat Jan 19, 2013 10:11 pm, edited 3 times in total.

For those that know
K: 0x2CD8D4F9

-Ninjex-
Moderator

Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)

### Re: Why does this not cause an overflow?

https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+Understand+integer+conversion+rules

tldr: magic

LOL

compare these two C programs:

Code: Select all
`#include <stdio.h>int main(void) {    unsigned short x = 65535;    printf("x was: %d\n", x);    x = x + 1;    printf("x is now: %d\n", x);    x = x - 65535;    printf("Bypass the overflow: %d\n", x + 65535);    return 0;}`

Code: Select all
`#include <stdio.h>int main(void) {    unsigned short x = 65535;    printf("x was: %d\n", x);    x = x + 1;    printf("x is now: %d\n", x);    x = x - 65535;    x = x + 65535;    printf("Bypass the overflow: %d\n", x);    return 0;}`

tgoe
Contributor

Posts: 715
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)

### Re: Why does this not cause an overflow?

Thanks for the help...
You are a saver Tgoe!

I removed the code blocks to allow the colour code to show my mistake.

-- Sat Jan 19, 2013 9:55 pm --

tgoe wrote:#include <stdio.h>

int main(void) {
unsigned short x = 65535;
printf("x was: %d\n", x);
x = x + 1;
printf("x is now: %d\n", x);
x = x - 65535;
printf("Bypass the overflow: %d\n", x + 65535);
return 0;
}

#include <stdio.h>

int main(void) {
unsigned short x = 65535;
printf("x was: %d\n", x);
x = x + 1;
printf("x is now: %d\n", x);
x = x - 65535;
x = x + 65535
;
printf("Bypass the overflow: %d\n", x);
return 0;
}

The second code calls for the expressions outside of the print statement
This works for namespace std

#include <iostream>

int main()
{
using namespace std;
unsigned short x = 65535; // largest 2-byte unsigned value possible
cout << "x was: " << x << endl;
x = x + 1; // We desire 65536, but we get overflow!
cout << "x is now: " << x << endl;
x -= 65535; x += 65535;
cout << "This will have the same result: ";
cout << x;
cout << endl;
}

#facepalm

For those that know
K: 0x2CD8D4F9

-Ninjex-
Moderator

Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)