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.
Español
Suma binaria
Se suma columna por columna de derecha a izquierda, llevándolos tal como se hace en decimal:
| Bit A |
Bit B |
Acarreo entrante |
Bit de suma |
Acarreo saliente |
| 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 |
Desbordamiento (Overflow) ocurre cuando el resultado necesita más bits de los que puede contener el registro; el acarreo saliente de la columna más a la izquierda es el bit de desbordamiento.
Ejemplo resuelto. Sume los enteros sin signo de 8 bits $10110101$ y $01101100$, y comente sobre el resultado.
$10110101 + 01101100 = 1\,00100001$. La respuesta necesita 9 bits, por lo que no cabe en un registro de 8 bits: ha ocurrido un desbordamiento. Una respuesta completa debe nombrar el error y explicar por qué, utilizando el tamaño de palabra dado en la pregunta: "Desbordamiento: el resultado real ($289$) es mayor que el valor máximo que puede contener un registro de 8 bits ($255$), por lo que el acarreo saliente del bit más significativo se pierde y el resultado almacenado ($00100001 = 33$) es incorrecto."
Resta binaria
La forma habitual es mediante la suma del complemento a dos: para realizar $A - B$, forme el complemento a dos de $B$ (invierta cada bit y sume 1), luego sume y descarte cualquier acarreo saliente final.
Para restar $00011110$ de $01100100$ (sin signo, 8 bits):
- Complemento a dos de $00011110$: invertir → $11100001$, sumar 1 → $11100010$.
- Sumar a $01100100$: resultado $1\,01000110$ (9 bits) — descarte el 1 inicial → $01000110 = 70_{10}$. Verificación: $100 - 30 = 70$. ✓
Enteros con signo en complemento a dos
En un número en complemento a dos de $n$ bits:
- El bit más significativo (MSB) es el bit de signo: 0 = positivo, 1 = negativo.
- Para leer un número negativo: invierta cada bit, sume 1 y luego cambie el signo.
Así, $11100010$ es negativo; invierte → $00011101$, suma 1 → $00011110 = 30$, por lo que es $-30$. Esto es un entero con signo (a diferencia de uno sin signo). El rango para $n$ bits es de $-2^{n-1}$ a $+2^{n-1} - 1$; para 8 bits, de $-128$ ($10000000$) a $+127$ ($01111111$).
Los mismos bits significan diferentes números dependiendo de la lectura acordada. Como un entero sin signo, cada bit es un valor posicional, por lo que 8 bits van desde $0$ hasta $255$; como un entero signado en complemento a dos, el bit superior es el signo, por lo que los mismos 8 bits van desde $-128$ hasta $+127$. El patrón $11111111$ representa $255$ leído de una manera y $-1$ leído de la otra —nada en los propios bits indica cuál es la correcta.
El mismo byte leído como sin signo y como signado: solo la interpretación acordada los distingue
Complemento a dos de 8 bits: el bit de signo divide el rango en negativo ($-128$ a $-1$) y positivo ($0$ a $127$)
Ejemplo resuelto. ¿Qué valor decimal representa el número en complemento a dos de 8 bits $10110100$?
El MSB es 1, por lo tanto es negativo. Invertir → $01001011$, sumar 1 → $01001100 = 76$, por lo tanto el valor es $-76$. Verificar con valores posicionales: $-128 + 32 + 16 + 4 = -76$.
Ejemplo resuelto. Escribir $-108$ como un entero en complemento a dos de 12 bits.
Empezar con $+108$ en 12 bits: $108 = 64 + 32 + 8 + 4$, por lo tanto 0000 0110 1100. Invertir cada bit: 1111 1001 0011. Sumar 1: 1111 1001 0100. Verificar con valores posicionales, donde el bit superior vale $-2^{11} = -2048$: $-2048 + 1024 + 512 + 256 + 128 + 16 + 4 = -108$. ✓
Para 12 bits, el rango va de $-2048$ (1000 0000 0000) hasta $+2047$ (0111 1111 1111). Las preguntas que solicitan los valores más pequeños y más grandes buscan estos dos patrones específicos, por lo que se debe aprender la regla: el número más negativo es un 1 seguido de ceros; el más positivo es un 0 seguido de unos.
Un corrimiento aritmético 算术移位 mueve cada bit hacia la izquierda o hacia la derecha pero mantiene el signo: un corrimiento a la derecha por una posición reduce a la mitad el valor y copia el bit de signo en el espacio vacío a la izquierda, por lo que un número negativo permanece negativo (1111 1001 0100 desplazado tres posiciones a la derecha es 1111 1111 0010, que equivale a $-14$: $-108 / 8 = -13.5$, y un corrimiento a la derecha redondea hacia abajo). Un corrimiento a la izquierda duplica el valor. Los corrimientos pertenecen al conjunto de instrucciones de ensamblaje en el tema 4, pero esta pregunta se formula en el contexto del trabajo numérico aquí tratado.
El desbordamiento en aritmética signada ocurre cuando el resultado real cae fuera de este rango —se detecta cuando el bit de signo cambia incorrectamente (dos positivos dando como resultado un negativo, o dos negativos dando como resultado un positivo).
Complemento a uno
Antes del complemento a dos, un esquema más antiguo llamado complemento a uno 反码 representaba un número negativo simplemente invertiendo todos los bits del positivo —no existe el paso de "sumar 1".
- $+30 = 00011110$, por lo tanto en complemento a uno $-30 = 11100001$ (solo el inverso).
- Desventaja: tiene dos ceros —$00000000$ ($+0$) y $11111111$ ($-0$)— lo cual desperdicia un patrón de bits y complica la aritmética.
El complemento a dos (invertir y sumar 1) elimina el cero negativo: tiene un único cero y permite que la suma y la resta utilicen el mismo circuito. Por eso las computadoras modernas almacenan enteros signados en complemento a dos, no en complemento a uno.