English
Binary addition
Add column by column from the right, carrying as in denary:
| Bit A |
Bit B |
Carry in |
Sum bit |
Carry out |
| 0 |
0 |
0 |
0 |
0 |
| 0 |
0 |
1 |
1 |
0 |
| 0 |
1 |
0 |
1 |
0 |
| 0 |
1 |
1 |
0 |
1 |
| 1 |
1 |
0 |
0 |
1 |
| 1 |
1 |
1 |
1 |
1 |
Overflow 溢出 happens when the result needs more bits than the register 寄存器 can hold — the carry-out of the leftmost column is the overflow bit.
Worked example. Add the 8-bit unsigned integers $10110101$ and $01101100$, and comment on the result.
$10110101 + 01101100 = 1\,00100001$. The answer needs 9 bits, so it does not fit in an 8-bit register: overflow has occurred. A full answer names the error and says why, using the word size the question gave: "Overflow: the true result ($289$) is larger than the largest value an 8-bit register can hold ($255$), so the carry out of the most significant bit is lost and the stored result ($00100001 = 33$) is wrong."
Binary subtraction
The usual way is two's complement 补码 addition: to do $A - B$, form the two's complement of $B$ (invert every bit and add 1), then add, and discard any final carry-out.
To subtract $00011110$ from $01100100$ (unsigned 8-bit):
- two's complement of $00011110$: invert → $11100001$, add 1 → $11100010$.
- add to $01100100$: result $1\,01000110$ (9 bits) — discard the leading 1 → $01000110 = 70_{10}$. Check: $100 - 30 = 70$. ✓
Two's complement signed integers
In an $n$-bit two's-complement number:
- the most significant bit 最高有效位 (MSB) is the sign bit 符号位: 0 = positive, 1 = negative.
- to read a negative number: invert every bit, add 1, then negate.
So $11100010$ is negative; invert → $00011101$, add 1 → $00011110 = 30$, so it is $-30$. This is a signed integer 有符号整数 (unlike an unsigned 无符号 one). The range for $n$ bits is $-2^{n-1}$ to $+2^{n-1} - 1$; for 8 bits, $-128$ ($10000000$) to $+127$ ($01111111$).
The same bits mean different numbers depending on the agreed reading. As an unsigned integer every bit is a place value, so 8 bits run from $0$ to $255$; as a signed two's-complement integer the top bit is the sign, so the same 8 bits run from $-128$ to $+127$. The pattern $11111111$ is $255$ read one way and $-1$ read the other — nothing in the bits themselves says which.
The same byte read as unsigned and as signed: only the agreed interpretation tells them apart
8-bit two's complement: the sign bit splits the range into negative ($-128$ to $-1$) and positive ($0$ to $127$)
Worked example. What denary value does the 8-bit two's-complement number $10110100$ represent?
The MSB is 1, so it is negative. Invert → $01001011$, add 1 → $01001100 = 76$, so the value is $-76$. Check with place values: $-128 + 32 + 16 + 4 = -76$.
Worked example. Write $-108$ as a 12-bit two's-complement integer.
Start from $+108$ in 12 bits: $108 = 64 + 32 + 8 + 4$, so 0000 0110 1100. Invert every bit: 1111 1001 0011. Add 1: 1111 1001 0100. Check with place values, where the top bit is worth $-2^{11} = -2048$: $-2048 + 1024 + 512 + 256 + 128 + 16 + 4 = -108$. ✓
For 12 bits the range is $-2048$ (1000 0000 0000) to $+2047$ (0111 1111 1111). Questions that ask for the smallest and largest values want these two patterns, so learn the rule: the most negative number is a 1 followed by zeros; the most positive is a 0 followed by ones.
An arithmetic shift 算术移位 moves every bit left or right but keeps the sign: a shift right by one place halves the value and copies the sign bit into the empty space on the left, so a negative number stays negative (1111 1001 0100 shifted right three places is 1111 1111 0010, which is $-14$: $-108 / 8 = -13.5$, and a shift right rounds down). A shift left doubles the value. Shifts belong to the assembly instruction set in topic 4, but this question is asked with the number work here.
Overflow in signed arithmetic happens when the true result falls outside this range — spotted when the sign bit flips wrongly (two positives giving a negative, or two negatives giving a positive).
One's complement
Before two's complement, an older scheme called one's complement 反码 represented a negative number by simply inverting every bit of the positive — there is no "add 1" step.
- $+30 = 00011110$, so in one's complement $-30 = 11100001$ (just the inverse).
- Drawback: it has two zeros — $00000000$ ($+0$) and $11111111$ ($-0$) — which wastes a bit pattern and makes arithmetic awkward.
Two's complement (invert and add 1) removes the negative zero: it has a single zero and lets addition and subtraction use the same circuit. That is why modern computers store signed integers in two's complement, not one's complement.
Português
Adição binária
Adicione coluna por coluna da direita, carregando como na decimal:
| Bit A |
Bit B |
Carry in |
Sum bit |
Carry out |
| 0 |
0 |
0 |
0 |
0 |
| 0 |
0 |
1 |
1 |
0 |
| 0 |
1 |
0 |
1 |
0 |
| 0 |
1 |
1 |
0 |
1 |
| 1 |
1 |
0 |
0 |
1 |
| 1 |
1 |
1 |
1 |
1 |
Overflow 溢出 ocorre quando o resultado precisa de mais bits do que o register 寄存器 pode armazenar — o carry-out da coluna mais à esquerda é o bit de overflow.
Exemplo resolvido. Some os inteiros sem sinal de 8 bits $10110101$ e $01101100$, e comente sobre o resultado.
$10110101 + 01101100 = 1\,00100001$. A resposta precisa de 9 bits, então não cabe em um registro de 8 bits: ocorreu overflow. Uma resposta completa nomeia o erro e diz por quê, usando a largura de palavra dada pela questão: "Overflow: o resultado verdadeiro ($289$) é maior que o maior valor que um registro de 8 bits pode armazenar ($255$), então o carry out do bit mais significativo é perdido e o resultado armazenado ($00100001 = 33$) está errado."
Subtração binária
O método usual é a adição de two's complement 补码: para fazer $A - B$, forme o two's complement de $B$ (inverte todos os bits e soma 1), depois some, e descarte qualquer carry-out final.
Para subtrair $00011110$ de $01100100$ (sem sinal 8 bits):
- two's complement de $00011110$: inverta → $11100001$, some 1 → $11100010$.
- some com $01100100$: resultado $1\,01000110$ (9 bits) — descarte o 1 inicial → $01000110 = 70_{10}$. Verifique: $100 - 30 = 70$. ✓
Inteiros sinalizados em two's complement
Em um número two's complement de $n$ bits:
- o most significant bit 最高有效位 (MSB) é o sign bit 符号位: 0 = positivo, 1 = negativo.
- para ler um número negativo: inverta todos os bits, some 1, depois negue.
Então $11100010$ é negativo; inverta → $00011101$, some 1 → $00011110 = 30$, logo é $-30$. Este é um signed integer 有符号整数 (ao contrário de um unsigned 无 signed one). O intervalo para $n$ bits é $-2^{n-1}$ a $+2^{n-1} - 1$; para 8 bits, $-128$ ($10000000$) a $+127$ ($01111111$).
Os mesmos bits significam números diferentes dependendo da leitura acordada. Como inteiro unsigned, cada bit é um valor posicional, então 8 bits variam de $0$ a $255$; como inteiro sinalizado two's complement, o bit superior é o sinal, então os mesmos 8 bits variam de $-128$ a $+127$. O padrão $11111111$ é $255$ lido de uma forma e $-1$ lido da outra — nada nos próprios bits diz qual é.
O mesmo byte lido como unsigned e signed: apenas a interpretação acordada os distingue
Two's complement de 8 bits: o sign bit divide o intervalo em negativo ($-128$ a $-1$) e positivo ($0$ a $127$)
Exemplo resolvido. Qual o valor decimal representado pelo número two's complement de 8 bits $10110100$?
O MSB é 1, então é negativo. Inverta → $01001011$, adicione 1 → $01001100 = 76$, então o valor é $-76$. Verifique com valores posicionais: $-128 + 32 + 16 + 4 = -76$.
Exemplo resolvido. Escreva $-108$ como um inteiro two's complement de 12 bits.
Comece com $+108$ em 12 bits: $108 = 64 + 32 + 8 + 4$, então 0000 0110 1100. Inverta todos os bits: 1111 1001 0011. Some 1: 1111 1001 0100. Verifique com valores posicionais, onde o bit superior vale $-2^{11} = -2048$: $-2048 + 1024 + 512 + 256 + 128 + 16 + 4 = -108$. ✓
Para 12 bits, o intervalo é $-2048$ (1000 0000 0000) a $+2047$ (0111 1111 1111). Questões que pedem os menores e maiores valores querem esses dois padrões, então aprenda a regra: o número mais negativo é um 1 seguido de zeros; o mais positivo é um 0 seguido de uns.
Um arithmetic shift 算术移位 move todos os bits para a esquerda ou direita, mas mantém o sinal: um shift right em um lugar divide o valor ao meio e copia o sign bit no espaço vazio à esquerda, então um número negativo permanece negativo (1111 1001 0100 deslocado right três lugares é 1111 1111 0010, que é $-14$: $-108 / 8 = -13.5$, e um shift right arredonda para baixo). Um shift left dobra o valor. Shifts pertencem ao conjunto de instruções assembly no tópico 4, mas esta pergunta é feita com o trabalho numérico aqui.
Overflow em aritmética sinalizada ocorre quando o resultado verdadeiro cai fora deste intervalo — identificado quando o sign bit inverte incorretamente (dois positivos dando negativo, ou dois negativos dando positivo).
One's complement
Antes do two's complement, um esquema antigo chamado one's complement 反码 representava um número negativo simplesmente invertendo todos os bits do positivo — não há etapa de "somar 1".
- $+30 = 00011110$, então em one's complement $-30 = 11100001$ (apenas o inverso).
- Desvantagem: possui dois zeros — $00000000$ ($+0$) e $11111111$ ($-0$) — o que desperdiça um padrão de bits e torna a aritmática complicada.
Two's complement (inverta e some 1) elimina o zero negativo: ele tem um único zero e permite que adição e subtração usem o mesmo circuito. É por isso que computadores modernos armazenam inteiros sinalizados em two's complement, não em one's complement.