# In a computer, why does adding one to the maximum integer result in zero?

I'm interested in the process of adding to computers.

How do computers add two numbers? Is this electronic magic? Is this an algorithm?

Also, I'm wondering why adding 1 to the maximum number (... 111) results in zero (... 000)? Asuming ... 111 is the binary representation of max int in this computer architecture.

Thank.

+3

source to share

This does not always result in 0. If you use a signed integer type in most representations, you will get the minimum value going from (say)

``````01111111 (127)
10000000 (-127)
```

```

On some architectures - and in some modes at least - this type of overflow is not meaningful, it raises an error. When it gives a value, it is basically still just a binary count. Imagine you have an 8 bit unsigned integer ... the last few values ​​would be

`````` 11111100
11111101
11111110
11111111
```

```

then the logical next step would be to jump to the next bit:

``````100000000
```

```

So the computer effectively does this, but then throws out that new high bit, keeping only the low 8 bits (because it's an 8 bit type).

You don't need to use computers to see this effect. Suppose you have an analog odometer in your car - a view with multiple "wheels". When you get to the end of your range, it just wraps around:

``````99997
99998
99999
00000 // Overflow!
```

```
+4

source

This is the case in an arithmetic processor that implements two's complement binary arithmetic for integers. This is not the case for, say, floating point numbers, and it is not necessarily true on rare computers that do math in other ways (signed values, etc.).

the two additions make some things easy on the hardware, such as implementing some signed and unsigned operations using the same logic.

Remember the value 1111 ... 1111 represents -1 under two's complement, and you want -1 to increment to 0.

1111111..111 vanishes "naturally". When you add 1, carry 1 propagates through the number, causing each digit to rotate by 0, and then it does the number (where it is discarded or used to set an overflow indication).

0

source

The add process is hardware related and is performed by the ALU on your CPU. Adding 1 to the maximum number will result in zero Because in binary, adding 1 to 1 will result in zero and 1 is added to the next digit So adding 1 to 11111 1 + 1 = 0 and 1 are shifted to the next digit, etc.

0

source

All Articles