On this page
Computers store whole numbers as rows of bits. Positive numbers are easy: 5 is 00000101. Negative numbers need a trick, and almost every machine uses the same one, called two's complement. Every step below is in 8 bits, so you can check it on paper. If binary itself is new, start with binary in Class 11 applied maths.
How do you write a negative number in two's complement?
To negate a number in two's complement, you invert every bit and add 1, as Thomas Finley's Cornell notes on two's complement describe it. Here is −3:
Write 3 in 8 bits: 00000011.
Flip every bit: 11111100.
Add 1: 11111101.
Run the same recipe on 11111101 and you get back 00000011, which is 3. The Java language specification, section 15.15.4 states the rule as an identity: for every integer x, −x equals (~x) + 1, where ~ flips the bits.
A faster way to read any pattern: give the leftmost bit a weight of −128 instead of +128. Then 11111101 is −128 + 64 + 32 + 16 + 8 + 4 + 1, which is −3.
What does the 8-bit two's complement table look like?
An 8-bit two's complement number runs from −128 to 127, the exact range the Java language specification, section 4.2.1 gives for its 8-bit byte type. Here are the landmarks:
01111111 is 127, the largest
01000000 is 64
00000001 is 1
00000000 is 0
11111111 is −1
11111110 is −2
11000000 is −64
10000001 is −127
10000000 is −128, the smallest
The leftmost bit gives the sign, 1 for negatives. There is only one zero, and the 256 patterns split into 128 negatives, zero and 127 positives. In general, an N-bit two's complement number runs from −2^(N−1) to 2^(N−1) − 1, and cppreference's page on C arithmetic types notes that since C23 this is the only signed representation the C standard allows.
Why does addition still work with negative numbers?
Add −3 and 5 exactly as you would add two positive numbers, column by column from the right:
−3 is 11111101
+5 is 00000101
The raw sum is 100000010, nine bits long
Keep the low 8 bits: 00000010, which is 2
No special case, no sign check. The reason is that 8 bits count round in a circle of 256. Since 11111101 is 256 − 3, adding it adds 256 and takes away 3, and the 256 is exactly the ninth bit that falls off the end. The Java specification, section 15.18.2 describes overflowing integer addition the same way: the result is the low-order bits of the true sum. Finley's Cornell notes give the payoff: the circuitry for addition and subtraction can be unified.
What happens when 127 + 1 overflows?
Add 01111111 and 00000001 and you get 10000000. In two's complement that is −128. Two positives made a negative: +128 does not exist in 8 bits, so the count wrapped round.
Nothing stops you. The Java specification, section 4.2.2 says its integer operators do not indicate overflow in any way, and section 15.18.2 adds that when overflow occurs, the sign of the result differs from the sign of the true sum. That gives you a rule you can use in an exam or in code:
Overflow is possible only when both numbers have the same sign.
It has happened when the answer's sign is different from theirs.
A positive plus a negative can never overflow.
Why is −(−128) still −128?
Negate 10000000 with the usual recipe. Flip the bits: 01111111. Add 1: 10000000. You are back where you started, because the answer, +128, lies one step past the largest number 8 bits can hold.
This is the one asymmetry of two's complement. Section 15.15.4 of the Java specification says that negating the most negative int or long gives that same number back, an overflow with no exception thrown. On paper, 8-bit −128 behaves the same way, so code that makes an int positive by negating it hands the most negative int back still negative.
Can you do these four in five minutes?
Write −1, −8 and −100 in 8 bits.
Add −8 and −100 in binary. Does it overflow?
Add 100 and 50 in binary. What do you get?
Negate −128 step by step and say why it fails.
Answers: −1 is 11111111, −8 is 11111000, −100 is 10011100. Then 11111000 + 10011100 keeps 10010100, which is −108, with no overflow. Next, 01100100 + 00110010 = 10010110, which reads as −106: two positives gave a negative, so it overflowed. Finally, −128 flips to 01111111 and returns to 10000000, because +128 is out of range.
If a step went wrong, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer. For the ideas around it, the Fermi Concept Library covers concepts in mathematics and computing. Next, see Gray code, where neighbouring numbers differ in one bit, or how the Hamming (7,4) code fixes any single error with 3 parity bits.



