Binary Arithmetic เลขคณิตฐานสอง
Add, subtract, multiply, and divide using only 0 and 1 บวก ลบ คูณ และหาร โดยใช้แค่ 0 และ 1
A processor does its arithmetic on binary values. The column method is exactly the one you use in denary (base 10) — work one column at a time from the right, and carry or borrow when a column overflows — but only the digits 0 and 1 are allowed. This lesson works through addition, subtraction, multiplication, and division one step at a time, then checks each answer in denary.
โปรเซสเซอร์คำนวณด้วยค่าฐานสอง วิธีคิดทีละหลักเหมือนกับที่เราใช้ในฐานสิบ (base 10) ทุกประการ — คิดทีละหลักจากขวาไปซ้าย และ “ทด” หรือ “ยืม” เมื่อหลักนั้นเกิน — ต่างกันแค่ใช้ได้เพียงหลัก 0 และ 1 บทนี้จะทำ การบวก การลบ การคูณ และ การหาร ทีละขั้น แล้วตรวจคำตอบด้วยเลขฐานสิบ
Every worked example below uses the same three denary values — 13,
11, 6, 5 — so you can compare the operations.
ตัวอย่างทุกข้อด้านล่างใช้ค่าฐานสิบชุดเดียวกัน — 13, 11,
6, 5 — เพื่อให้เทียบการดำเนินการต่าง ๆ ได้ง่าย
1. Binary addition 1. การบวกฐานสอง
There are only four single-bit sums. When the total reaches 2 it does not
fit in one binary digit, so you write 0 and carry 1 to the
next column on the left — the same idea as carrying in denary when a column reaches 10.
การบวกทีละบิตมีแค่ 4 กรณี เมื่อผลรวมถึง 2 จะใส่ในหลักเดียวไม่พอ จึงเขียน
0 แล้ว ทด 1 ไปหลักถัดไปทางซ้าย — แนวคิดเดียวกับการทดในฐานสิบ
เมื่อหลักหนึ่งถึง 10
| Column sum ผลบวกในหลัก | Result ผลลัพธ์ | Write / carry เขียน / ทด |
|---|---|---|
0 + 0 | 0 | write 0เขียน 0 |
0 + 1 | 1 | write 1เขียน 1 |
1 + 1 | 10 | write 0, carry 1เขียน 0 ทด 1 |
1 + 1 + 1 | 11 | write 1, carry 1เขียน 1 ทด 1 |
Worked example: 1101 + 1011 (denary 13 + 11). Each column is one step, right to left; the top row is the carry into that column.
ตัวอย่างที่ทำทีละขั้น: 1101 + 1011 (ฐานสิบ 13 + 11) แต่ละหลักคือหนึ่งขั้น จากขวาไปซ้าย แถวบนสุดคือค่าที่ทดเข้ามาในหลักนั้น
| Place valueค่าประจำหลัก | \(2^4\) | \(2^3\) | \(2^2\) | \(2^1\) | \(2^0\) |
|---|---|---|---|---|---|
| Carryตัวทด | 1 | 1 | 0 | 1 | – |
| First number (13)ตัวตั้ง (13) | 1 | 1 | 0 | 1 |
|
| Second number (11)ตัวบวก (11) | 1 | 0 | 1 | 1 |
|
| Sumผลบวก | 1 | 1 | 0 | 0 | 0 |
- Column \(2^0\):
1 + 1 = 10→ write0, carry1.หลัก \(2^0\):1 + 1 = 10→ เขียน0ทด1 - Column \(2^1\):
0 + 1 + 1(carry)= 10→ write0, carry1.หลัก \(2^1\):0 + 1 + 1(ตัวทด)= 10→ เขียน0ทด1 - Column \(2^2\):
1 + 0 + 1(carry)= 10→ write0, carry1.หลัก \(2^2\):1 + 0 + 1(ตัวทด)= 10→ เขียน0ทด1 - Column \(2^3\):
1 + 1 + 1(carry)= 11→ write1, carry1.หลัก \(2^3\):1 + 1 + 1(ตัวทด)= 11→ เขียน1ทด1 - Column \(2^4\): the final carry
1becomes the leftmost digit.หลัก \(2^4\): ตัวทดสุดท้าย1กลายเป็นหลักซ้ายสุด
Result: 1101 + 1011 = 11000. Check in denary: \(13 + 11 = 24\), and \(11000_2 = 16 + 8 = 24\). ✓
ผลลัพธ์: 1101 + 1011 = 11000 ตรวจด้วยฐานสิบ: \(13 + 11 = 24\) และ \(11000_2 = 16 + 8 = 24\) ✓
2. Binary subtraction (borrow method) 2. การลบฐานสอง (วิธียืม)
Subtract column by column. Three cases are easy: 0 - 0 = 0,
1 - 0 = 1, 1 - 1 = 0. The fourth, 0 - 1, cannot be
done directly — borrow 1 from the next column on the left. The
borrowed 1 is worth 2 in this column, so the column becomes
10 - 1 = 1, and the left column loses 1. Same idea as borrowing in denary.
ลบทีละหลัก สามกรณีง่าย: 0 - 0 = 0, 1 - 0 = 1, 1 - 1 = 0
ส่วนกรณีที่สี่ 0 - 1 ทำตรง ๆ ไม่ได้ ต้อง ยืม 1 จากหลักถัดไปทางซ้าย
ตัว 1 ที่ยืมมามีค่าเท่ากับ 2 ในหลักนี้ หลักนี้จึงกลายเป็น
10 - 1 = 1 และหลักซ้ายถูกหักออก 1 แนวคิดเดียวกับการยืมในฐานสิบ
Worked example: 1011 − 0101 (denary 11 − 5). The top row shows where a borrow happens.
ตัวอย่างที่ทำทีละขั้น: 1011 − 0101 (ฐานสิบ 11 − 5) แถวบนสุดแสดงว่าหลักใดต้องยืม
| Place valueค่าประจำหลัก | \(2^3\) | \(2^2\) | \(2^1\) | \(2^0\) |
|---|---|---|---|---|
| Borrowการยืม | −1 | |||
| First number (11)ตัวตั้ง (11) | 1 | 0 | 1 | 1 |
| Second number (5)ตัวลบ (5) | 0 | 1 | 0 | 1 |
| Differenceผลต่าง | 0 | 1 | 1 | 0 |
- Column \(2^0\):
1 - 1 = 0.หลัก \(2^0\):1 - 1 = 0 - Column \(2^1\):
1 - 0 = 1.หลัก \(2^1\):1 - 0 = 1 - Column \(2^2\):
0 - 1→ borrow from \(2^3\). Column becomes10 - 1 = 1.หลัก \(2^2\):0 - 1→ ยืมจากหลัก \(2^3\) หลักนี้กลายเป็น10 - 1 = 1 - Column \(2^3\): the
1was reduced to0by the borrow, so0 - 0 = 0.หลัก \(2^3\): เลข1ถูกลดเป็น0เพราะการยืม จึงเป็น0 - 0 = 0
Result: 1011 − 0101 = 0110. Check: \(11 - 5 = 6\), and \(0110_2 = 4 + 2 = 6\). ✓
ผลลัพธ์: 1011 − 0101 = 0110 ตรวจ: \(11 - 5 = 6\) และ \(0110_2 = 4 + 2 = 6\) ✓
Note: subtraction by adding the two's complement หมายเหตุ: การลบด้วยการบวกทูส์คอมพลีเมนต์
Processors usually do not build a separate “borrow” circuit. Instead they
compute a − b as a + (−b), where −b is the
two's complement of b (invert every bit, then add 1 —
see the previous lesson). The single carry-out of the top column is discarded.
โปรเซสเซอร์มักไม่สร้างวงจร “ยืม” แยกต่างหาก แต่คำนวณ a − b เป็น
a + (−b) โดย −b คือ ทูส์คอมพลีเมนต์ ของ
b (กลับทุกบิตแล้วบวก 1 — ดูบทก่อนหน้า) ตัวทดออกจากหลักบนสุดจะถูกทิ้งไป
Same example in 4 bits: \(11 - 5\). ตัวอย่างเดิมในระบบ 4 บิต: \(11 - 5\)
| Stepขั้น | Bits (4-bit)บิต (4 บิต) |
|---|---|
+5+5 | 0101 |
| Invert every bitกลับทุกบิต | 1010 |
Add 1 → −5บวก 1 → −5 | 1011 |
11 + −5 = 1011 + 101111 + −5 = 1011 + 1011 | 1 0110 |
| Drop the carry-out → keep 4 bitsทิ้งตัวทดออก → เก็บ 4 บิต | 0110 = 6 |
Same answer as the borrow method: 0110 = 6.
ได้คำตอบเดียวกับวิธียืม: 0110 = 6
3. Binary multiplication (shift-and-add) 3. การคูณฐานสอง (shift-and-add)
Long multiplication is simple in binary because each digit of the multiplier is only
0 or 1:
การคูณแบบตั้งหารทำได้ง่ายในฐานสอง เพราะแต่ละหลักของตัวคูณเป็นได้แค่ 0 หรือ 1
- Multiplier bit
0→ partial product is all zeros.บิตตัวคูณเป็น0→ ผลคูณย่อยเป็นศูนย์ทั้งหมด - Multiplier bit
1→ partial product is a copy of the multiplicand.บิตตัวคูณเป็น1→ ผลคูณย่อยคือ สำเนาของตัวตั้ง - Each partial product is shifted left by the position of its bit, then all are added.ผลคูณย่อยแต่ละตัวถูก เลื่อนซ้าย ตามตำแหน่งของบิตนั้น แล้วนำมาบวกกันทั้งหมด
Worked example: 110 × 101 (denary 6 × 5).
ตัวอย่างที่ทำทีละขั้น: 110 × 101 (ฐานสิบ 6 × 5)
| Stepขั้น | \(2^4\) | \(2^3\) | \(2^2\) | \(2^1\) | \(2^0\) |
|---|---|---|---|---|---|
Bit \(2^0\) of 101 is 1 → copy 110บิต \(2^0\) ของ 101 เป็น 1 → สำเนา 110 |
1 | 1 | 0 |
||
Bit \(2^1\) of 101 is 0 → zeros, shift 1บิต \(2^1\) ของ 101 เป็น 0 → ศูนย์ เลื่อน 1 |
0 | 0 | 0 | ||
Bit \(2^2\) of 101 is 1 → copy 110, shift 2บิต \(2^2\) ของ 101 เป็น 1 → สำเนา 110 เลื่อน 2 |
1 | 1 | 0 | ||
| Add the partial productsบวกผลคูณย่อยทั้งหมด | 1 | 1 | 1 | 1 | 0 |
Result: 110 × 101 = 11110. Check: \(6 \times 5 = 30\), and \(11110_2 = 16 + 8 + 4 + 2 = 30\). ✓
ผลลัพธ์: 110 × 101 = 11110 ตรวจ: \(6 \times 5 = 30\) และ \(11110_2 = 16 + 8 + 4 + 2 = 30\) ✓
Shortcut: multiplying by \(2^k\) just appends \(k\) zeros (a left shift). For example 110 × 10 = 1100 (\(6 \times 2 = 12\)).
ทางลัด: การคูณด้วย \(2^k\) คือการเติม 0 ต่อท้าย \(k\) ตัว (เลื่อนซ้าย) เช่น 110 × 10 = 1100 (\(6 \times 2 = 12\))
4. Binary division 4. การหารฐานสอง
There are two ways to divide, used for different jobs:
การหารมี 2 วิธี ใช้กับงานคนละแบบ:
-
Long division — works for any divisor. Gives a quotient and a remainder. This is what a general divide instruction (or
//and%in code) does. การตั้งหาร — ใช้ได้กับตัวหารทุกค่า ได้ ผลหาร และ เศษ เป็นสิ่งที่คำสั่งหารทั่วไป (หรือ//และ%ในโค้ด) ทำ - Right shift — only for dividing by a power of two, \(2^k\). Drop the rightmost \(k\) bits; those dropped bits are the remainder. Very fast, so hardware and compilers use it whenever the divisor is \(2, 4, 8, \dots\) การเลื่อนขวา — ใช้ได้เฉพาะการหารด้วยเลขยกกำลังสอง \(2^k\) ตัดบิตขวาสุดออก \(k\) ตัว บิตที่ตัดออกคือเศษ ทำได้เร็วมาก ฮาร์ดแวร์และคอมไพเลอร์จึงใช้วิธีนี้เมื่อตัวหารเป็น \(2, 4, 8, \dots\)
4a. Long division — any divisor 4a. การตั้งหาร — ตัวหารใด ๆ
Same routine as denary long division, but every “does it fit?” check has only
two answers. Bring bits of the dividend down one at a time into a running value; if the
running value is ≥ the divisor, the next quotient bit is 1
and you subtract the divisor; otherwise the quotient bit is 0.
ขั้นตอนเหมือนการตั้งหารฐานสิบ แต่การถามว่า “หารลงตัวไหม” มีคำตอบแค่สองแบบ
ดึงบิตของตัวตั้งลงมาทีละบิตเป็นค่าที่กำลังคิดอยู่ ถ้าค่านั้น
≥ ตัวหาร บิตผลหารถัดไปเป็น 1 แล้วลบตัวหารออก มิฉะนั้นบิตผลหารเป็น 0
Worked example: 11110 ÷ 101 (denary 30 ÷ 5).
ตัวอย่างที่ทำทีละขั้น: 11110 ÷ 101 (ฐานสิบ 30 ÷ 5)
| Bit brought downบิตที่ดึงลงมา | Running valueค่าที่กำลังคิด | ≥ 101 (5)?≥ 101 (5) ไหม? |
Quotient bitบิตผลหาร | After subtractingหลังลบ |
|---|---|---|---|---|
1 | 1 (1) | noไม่ | 0 | 1 (1) |
1 | 11 (3) | noไม่ | 0 | 11 (3) |
1 | 111 (7) | yesใช่ | 1 | 111 − 101 = 10 (2) |
1 | 101 (5) | yesใช่ | 1 | 101 − 101 = 0 (0) |
0 | 0 (0) | noไม่ | 0 | 0 (0) |
Result: quotient 00110 = 110, remainder 0. Check: \(30 \div 5 = 6\), and \(110_2 = 6\). ✓ (This is the multiplication example run backwards.)
ผลลัพธ์: ผลหาร 00110 = 110 เศษ 0 ตรวจ: \(30 \div 5 = 6\) และ \(110_2 = 6\) ✓ (เป็นตัวอย่างการคูณที่ทำย้อนกลับ)
Second example with a remainder: 1011 ÷ 10 (denary 11 ÷ 2).
ตัวอย่างที่สองที่มีเศษ: 1011 ÷ 10 (ฐานสิบ 11 ÷ 2)
| Bit brought downบิตที่ดึงลงมา | Running valueค่าที่กำลังคิด | ≥ 10 (2)?≥ 10 (2) ไหม? |
Quotient bitบิตผลหาร | After subtractingหลังลบ |
|---|---|---|---|---|
1 | 1 (1) | noไม่ | 0 | 1 (1) |
0 | 10 (2) | yesใช่ | 1 | 10 − 10 = 0 (0) |
1 | 1 (1) | noไม่ | 0 | 1 (1) |
1 | 11 (3) | yesใช่ | 1 | 11 − 10 = 1 (1) |
Result: quotient 0101 = 101, remainder 1. Check: \(11 \div 2 = 5\) remainder \(1\). ✓
ผลลัพธ์: ผลหาร 0101 = 101 เศษ 1 ตรวจ: \(11 \div 2 = 5\) เศษ \(1\) ✓
4b. Right shift — dividing by a power of two 4b. การเลื่อนขวา — หารด้วยเลขยกกำลังสอง
Dividing by \(2^k\) means throwing away the rightmost \(k\) bits. The bits you throw away are the remainder; what is left is the quotient. It is the mirror image of the multiplication shortcut (append zeros to multiply, drop digits to divide).
การหารด้วย \(2^k\) คือการตัดบิตขวาสุดทิ้ง \(k\) ตัว บิตที่ตัดทิ้งคือเศษ ส่วนที่เหลือคือผลหาร เป็นภาพสะท้อนของทางลัดการคูณ (เติมศูนย์เพื่อคูณ ตัดหลักออกเพื่อหาร)
Worked example: 1011 (denary 11) divided by 10 (\(2^1\)) and by 100 (\(2^2\)).
ตัวอย่างที่ทำทีละขั้น: 1011 (ฐานสิบ 11) หารด้วย 10 (\(2^1\)) และด้วย 100 (\(2^2\))
| Divide byหารด้วย | Dropตัดออก | Quotient (bits kept)ผลหาร (บิตที่เหลือ) | Remainder (bits dropped)เศษ (บิตที่ตัดออก) | Denary checkตรวจฐานสิบ |
|---|---|---|---|---|
10 (\(2^1\)) |
last 1 bitบิตท้าย 1 ตัว | 101 (5) |
1 (1) |
\(11 = 5 \times 2 + 1\) |
100 (\(2^2\)) |
last 2 bitsบิตท้าย 2 ตัว | 10 (2) |
11 (3) |
\(11 = 2 \times 4 + 3\) |
The 10 row matches the long-division example above — same quotient 101 and remainder 1, but found by inspection instead of five steps.
แถว 10 ตรงกับตัวอย่างการตั้งหารด้านบน — ผลหาร 101 และเศษ 1 เท่ากัน แต่ได้จากการดูเฉย ๆ แทนที่จะทำห้าขั้น
Check your work with Python ตรวจคำตอบด้วย Python
The by-hand methods above are the point of this lesson. These demos just confirm the same
answers. bin(x) shows a value in binary with a 0b prefix,
// is integer division (the quotient), and % is the remainder.
วิธีทำด้วยมือด้านบนคือหัวใจของบทนี้ ตัวอย่างด้านล่างไว้ยืนยันคำตอบเดียวกัน
bin(x) แสดงค่าเป็นฐานสองโดยขึ้นต้นด้วย 0b, // คือการหารเอาผลหาร
(จำนวนเต็ม) และ % คือเศษ
1 Addition: 1101 + 1011
1 การบวก: 1101 + 1011
Denary 13 + 11. The result 11000 is 0b11000, i.e. 24.
ฐานสิบ 13 + 11 ผลลัพธ์ 11000 คือ 0b11000 หรือ 24
0b11000 24
2 Subtraction: 1011 − 0101
2 การลบ: 1011 − 0101
Denary 11 − 5. The borrow-method result 0110 is 0b110, i.e. 6.
ฐานสิบ 11 − 5 ผลลัพธ์จากวิธียืม 0110 คือ 0b110 หรือ 6
0b110 6
3 Subtraction by adding the two's complement 3 การลบด้วยการบวกทูส์คอมพลีเมนต์
(2 ** 4) - 5 is the two's complement of 5 in 4 bits (1011).
Add it to 11, then % (2 ** 4) keeps just 4 bits (drops the carry-out).
The answer matches demo 2: 6.
(2 ** 4) - 5 คือทูส์คอมพลีเมนต์ของ 5 ในระบบ 4 บิต (1011)
นำไปบวกกับ 11 แล้ว % (2 ** 4) เก็บไว้แค่ 4 บิต (ตัดตัวทดออกทิ้ง)
คำตอบตรงกับตัวอย่างที่ 2: 6
0b1011 22 6
4 Multiplication: 110 × 101
4 การคูณ: 110 × 101
Denary 6 × 5. The shift-and-add result 11110 is 0b11110, i.e. 30.
Multiplying by 10 (2) just shifts left: 1100 = 12.
ฐานสิบ 6 × 5 ผลลัพธ์จาก shift-and-add 11110 คือ 0b11110 หรือ 30
การคูณด้วย 10 (2) คือการเลื่อนซ้าย: 1100 = 12
0b11110 30 0b1100
5 Long division: quotient and remainder 5 การตั้งหาร: ผลหารและเศษ
30 // 5 is the quotient and 30 % 5 the remainder for
11110 ÷ 101. The second pair is 1011 ÷ 10 (11 ÷ 2),
which has remainder 1.
30 // 5 คือผลหาร และ 30 % 5 คือเศษ ของ 11110 ÷ 101
คู่ที่สองคือ 1011 ÷ 10 (11 ÷ 2) ซึ่งมีเศษ 1
0b110 0 0b101 1
6 Division by a power of two = right shift 6 การหารด้วยเลขยกกำลังสอง = การเลื่อนขวา
n = 11 is 1011. Dividing by 2 drops the last bit
(101, remainder 1); dividing by 4 drops the last two
(10, remainder 11).
n = 11 คือ 1011 หารด้วย 2 คือตัดบิตท้ายออก 1 ตัว
(101 เศษ 1) หารด้วย 4 คือตัดบิตท้ายออก 2 ตัว
(10 เศษ 11)
0b1011 0b101 1 0b10 3
Note: Run uses a JavaScript simulator, not real Python. For real Python, try Google Colab. หมายเหตุ: ปุ่ม Run จำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการ Python จริง ลองใช้ Google Colab
References References / แหล่งอ้างอิง
Sources used for binary arithmetic:
แหล่งที่ใช้ประกอบเนื้อหาเลขคณิตฐานสอง:
- Binary arithmetic — Wikipedia — Binary number (arithmetic) (addition, subtraction, multiplication, long division) (การบวก ลบ คูณ และการตั้งหาร)
- Division algorithm — Wikipedia — Division algorithm (quotient and remainder, shift-and-subtract) (ผลหารและเศษ วิธี shift-and-subtract)
- Arithmetic shift — Wikipedia — Arithmetic shift (multiply / divide by a power of two by shifting) (คูณ / หารด้วยเลขยกกำลังสองด้วยการเลื่อนบิต)
-
Python
bin— Built-in Functions — bin (used to inspect demo results) (ใช้ดูผลในตัวอย่าง Run)