In Mathematics, if you add a positive number Y to X, then X+Y is always greater than X.

In Computers, this is not always the case. Here is an example. See if you can find the problem. The answer is hidden below.

Javascript

PHP

X

X+10

123456789

123456799

9223372036854775807

9.2233720368548E+18

-9223372036854775808

-9223372036854775798

Python

Java

C

Look carefully at the middle Java row. Notice anything different about X+10? How can X+10 be negative when X is positive?

For integer arithmetic, most computers do not check for overflow.
If the number is too big to fit, they just throw the extra bits.

Most computers use what is called "two's complement arithemetic".
Let's consider a computer with just 3 bits, but the same principle applies for 16, 32 or 64 bit computers.
In a three bit computer, only 2^{3} (8) values are possible.
So the only numbers available are -4 to 3, as shown here, in the first two columns.

Bits

Number

Add 1

Result

0 1 1

3

1 0 0

-4

0 1 0

2

0 1 1

3

0 0 1

1

0 1 0

2

0 0 0

0

0 0 1

1

1 1 1

-1

0 0 0

0

1 1 0

-2

1 1 1

-1

1 0 1

-3

1 1 0

-2

1 0 0

-4

1 0 1

-3

Numbers with a decimal point or exponent do not suffer from this problem, although they do have other issues.