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.
ไทย
การบวกไบนารี
บวกคอลัมน์ต่อคอลัมน์จากขวาไปซ้าย ทำ Carry เหมือนทศนิยม:
| 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 เกิดขึ้นเมื่อผลลัพธ์ต้องการ Bits มากกว่าที่ register จะเก็บได้ — Carry-out ของคอลัมน์ซ้ายสุดคือ Overflow bit
ตัวอย่างวิธีทำ. บวก unsigned integers 8-bit $10110101$ และ $01101100$ และอธิบายผลลัพธ์
$10110101 + 01101100 = 1\,00100001$. คำตอบต้องใช้ 9 บิต ดังนั้นจึงไม่พอดีกับรีจิสเตอร์ขนาด 8 บิต: เกิดการล้น (overflow) คำตอบที่สมบูรณ์ต้องระบุชื่อข้อผิดพลาดและอธิบายเหตุผล โดยใช้ขนาดบิตที่โจทย์กำหนดไว้: "ล้น: ผลลัพธ์ที่แท้จริง ($289$) มีค่ามากกว่าค่าสูงสุดที่รีจิสเตอร์ขนาด 8 บิตสามารถเก็บได้ ($255$) ทำให้ carry ที่ออกจากบิตที่มีค่านัยสำคัญสูงสุด (MSB) หายไป และผลลัพธ์ที่บันทึกไว้ ($00100001 = 33$) จึงผิดเพี้ยนไป"
การลบเลขฐานสอง
วิธีทั่วไปคือ การบวกด้วยสองส่วนเสริม (two's complement): เพื่อทำ $A - B$ ให้สร้างสองส่วนเสริมของ $B$ (กลับทุกบิตแล้วบวก 1) จากนั้นทำการบวก และทิ้ง carry ที่เกิดขึ้นในขั้นสุดท้าย
ในการลบ $00011110$ ออกจาก $01100100$ (unsigned 8-bit):
- สองส่วนเสริมของ $00011110$: กลับ → $11100001$, บวก 1 → $11100010$.
- บวกเข้ากับ $01100100$: ได้ผลลัพธ์ $1\,01000110$ (9 บิต) — ทิ้งเลข 1 ตัวแรกออก → $01000110 = 70_{10}$. ตรวจสอบ: $100 - 30 = 70$. ✓
จำนวนเต็มมีเครื่องหมายแบบสองส่วนเสริม (Two's complement signed integers)
ในจำนวนสองส่วนเสริมขนาด $n$-บิต:
- บิตที่มีค่านัยสำคัญสูงสุด (MSB) คือ บิตบอกเครื่องหมาย: 0 = บวก, 1 = ลบ.
- ในการอ่านค่าลบ: กลับทุกบิต, บวก 1, แล้วเปลี่ยนเครื่องหมายเป็นลบ
ดังนั้น $11100010$ จึงเป็นค่าลบ; กลับ → $00011101$, บวก 1 → $00011110 = 30$, ดังนั้นค่านี้คือ $-30$. นี่คือ จำนวนเต็มมีเครื่องหมาย (ต่างจาก unsigned) ช่วงของค่าสำหรับ $n$ บิต คือ $-2^{n-1}$ ถึง $+2^{n-1} - 1$; สำหรับ 8 บิต คือ $-128$ ($10000000$) ถึง $+127$ ($01111111$).
บิตชุดเดียวกันหมายถึงตัวเลขต่างกันขึ้นอยู่กับวิธีการอ่าน agreed together. ในฐานะ จำนวนเต็มไม่มีเครื่องหมาย ทุกบิตเป็นตำแหน่งค่า, ดังนั้น 8 บิตจึงครอบคลุมตั้งแต่ $0$ ถึง $255$; ในฐานะ จำนวนเต็ม signed แบบ two's complement บิตบนสุดคือเครื่องหมาย, ดังนั้น 8 บิตเดียวกันจึงครอบคลุมตั้งแต่ $-128$ ถึง $+127$. ลวดลาย $11111111$ คือ $255$ เมื่ออ่านทางหนึ่งและ $-1$ เมื่ออ่านอีกทาง — ไม่มีอะไรในบิตเองบอกได้ว่าอันไหน
byte เดียวกันที่อ่านเป็น unsigned และ signed: การตีความที่ตกลงกันเท่านั้นที่จะแยกแยะได้
สองส่วนเสริม 8 บิต: บิตบอกเครื่องหมายแบ่งช่วงค่าให้เป็นลบ ($-128$ ถึง $-1$) และบวก ($0$ ถึง $127$)
ตัวอย่างวิธีทำ. ค่าฐานสิบใดที่จำนวนสองส่วนเสริม 8 บิต $10110100$ แทนอยู่?
MSB เป็น 1 ดังนั้นจึงเป็นลบ. กลับ → $01001011$, บวก 1 → $01001100 = 76$, ดังนั้นค่าคือ $-76$. ตรวจสอบด้วยตำแหน่งของค่า: $-128 + 32 + 16 + 4 = -76$.
ตัวอย่างวิธีทำ. เขียน $-108$ เป็นจำนวนเต็มสองส่วนเสริมขนาด 12 บิต
เริ่มต้นจาก $+108$ ใน 12 บิต: $108 = 64 + 32 + 8 + 4$, ดังนั้น 0000 0110 1100. กลับทุกบิต: 1111 1001 0011. บวก 1: 1111 1001 0100. ตรวจสอบด้วยตำแหน่งของค่า ซึ่งบิตบนสุดมีค่าเท่ากับ $-2^{11} = -2048$: $-2048 + 1024 + 512 + 256 + 128 + 16 + 4 = -108$. ✓
สำหรับ 12 บิต ช่วงค่าคือ $-2048$ (1000 0000 0000) ถึง $+2047$ (0111 1111 1111). คำถามที่ถามหาค่าที่น้อยที่สุดและมากที่สุดต้องการรูปแบบเหล่านี้สองรูปแบบ ดังนั้นให้จดจำกฎ: เลขที่น้อยที่สุดคือ 1 ตามด้วยศูนย์; เลขที่สูงที่สุดคือ 0 ตามด้วยหนึ่ง
การเลื่อนเลขคณิต (arithmetic shift) จะเลื่อนทุกบิตไปทางซ้ายหรือขวาแต่คงเครื่องหมายไว้: การเลื่อนไปทางขวาหนึ่งตำแหน่งจะทำให้ค่าลดลงครึ่งหนึ่งและคัดลอกบิตบอกเครื่องหมายเข้าไปในช่องว่างด้านซ้าย ทำให้เลขลบยังคงเป็นลบ (1111 1001 0100 เลื่อนไปทางขวาสามตำแหน่งจะได้ 1111 1111 0010 ซึ่งเป็น $-14$: $-108 / 8 = -13.5$, และการเลื่อนไปทางขวาเป็นการปัดเศษลง) การเลื่อนไปทางซ้ายจะทำให้ค่าเพิ่มขึ้นเป็นสองเท่า การเลื่อนบิตเป็นคำสั่งในภาษาอัสเซมเบิล러ในหัวข้อ 4 แต่คำถามนี้ถูกตั้งขึ้นเพื่อทดสอบความรู้เรื่องการคำนวณตัวเลข
การล้นในการคำนวณเลขคณิตแบบมีเครื่องหมายเกิดขึ้นเมื่อผลลัพธ์จริงตกนอกช่วงนี้ — ตรวจพบได้เมื่อบิตบอกเครื่องหมายเปลี่ยนไปอย่างผิดปกติ (บวกสองตัวให้ผลลบ หรือลบสองตัวให้ผลบวก)
ส่วนเสริมหนึ่ง (One's complement)
ก่อนหน้าสองส่วนเสริม จะมีระบบเก่าที่เรียกว่า ส่วนเสริมหนึ่ง ซึ่งใช้แทนเลขลบด้วยการ กลับทุกบิต ของเลขบวกโดยตรง — ไม่มีขั้นตอน "บวก 1"
- $+30 = 00011110$, ดังนั้นในส่วนเสริมหนึ่ง $-30 = 11100001$ (แค่กลับค่า)
- ข้อเสีย: มันมี ศูนย์สองตัว — $00000000$ ($+0$) และ $11111111$ ($-0$) — ซึ่งเปลืองรูปแบบบิตและทำให้การคำนวณยุ่งยาก
สองส่วนเสริม (กลับ และ บวก 1) ช่วยกำจัดศูนย์ลบ: มีเพียงศูนย์เดียวและช่วยให้การบวกและการลบใช้วงจรเดียวกัน นั่นคือเหตุผลว่าทำไมคอมพิวเตอร์สมัยใหม่จึงเก็บจำนวนเต็มมีเครื่องหมายในรูปแบบสองส่วนเสริม ไม่ใช่ส่วนเสริมหนึ่ง