Binary, Denary, and Hexadecimal เลขฐานสอง ฐานสิบ และฐานสิบหก
Place values in base-10, base-2, and base-16 ค่าประจำหลักในระบบฐาน 10, ฐาน 2 และฐาน 16
A number system uses a fixed set of digits and place values
based on powers of the base. Denary (decimal) is base-10, binary is base-2, and hexadecimal is base-16.
Computers store bits, so binary is natural; hex is a compact way to write byte-sized values.
The same value can be written in each base — for example, 255 in denary is
11111111 in binary and FF in hexadecimal.
ระบบเลขใช้ชุดหลักที่จำกัด และมีค่าประจำหลักเป็นกำลังของฐาน
ฐานสิบ (denary/decimal) คือ base-10 ฐานสองคือ base-2 และฐานสิบหกคือ base-16
คอมพิวเตอร์เก็บบิต จึงใช้ฐานสองเป็นหลัก ส่วนฐานสิบหกช่วยเขียนค่าไบต์ให้สั้นลง
ค่าเดียวกันเขียนได้หลายฐาน — เช่น 255 ฐานสิบ คือ 11111111 ฐานสอง และ FF ฐานสิบหก
Digit positions show place values หลักของตัวเลขคือตำแหน่งของตัวเลข
The number 255 uses three digits in denary, eight bits in binary, and two hex digits. Empty cells mean that place is not used in that representation.
ตัวเลข 255 ใช้ 3 หลักในฐานสิบ 8 บิตในฐานสอง และ 2 หลักในฐานสิบหก ช่องว่างหมายถึงหลักนั้นไม่ได้ใช้ในการเขียนแบบนั้น
| Place (N) หลัก (N) | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Denary (base 10) ฐานสิบ | — | — | — | — | — | 2 | 5 | 5 |
| Binary (base 2) ฐานสอง | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| Hexadecimal (base 16) ฐานสิบหก | — | — | — | — | — | — | F | F |
Digits and bases หลักที่ใช้ในแต่ละฐาน
- Denary (base-10): digits 0–9; place values \(10^N\) ฐานสิบ (base-10): หลัก 0–9; ค่าประจำหลัก \(10^N\)
-
Binary (base-2): digits 0 and 1 only; place values \(2^N\) (often written
0b…) ฐานสอง (base-2): ใช้ได้แค่ 0 และ 1; ค่าประจำหลัก \(2^N\) (มักเขียน0b…) -
Hexadecimal (base-16): digits 0–9 and A–F (10–15); place values \(16^N\) (often
0x…) ฐานสิบหก (base-16): หลัก 0–9 และ A–F (แทน 10–15); ค่าประจำหลัก \(16^N\) (มักเขียน0x…)
Place-value calculation example ตัวอย่างการคำนวณค่าในแต่ลัก
Each digit is multiplied by its place value; the sum equals 255 in every base below.
แต่ละหลักคูณด้วยค่าประจำหลักของตำแหน่งนั้น แล้วนำมารวมกันได้ 255 ในทุกฐานด้านล่าง
| Base ฐาน | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | Total รวม |
|---|---|---|---|---|---|---|---|---|---|
| Denary (base 10) ฐานสิบ | — | — | — | — | — | \(2 \times 10^2\) | \(5 \times 10^1\) | \(5 \times 10^0\) | 255 |
| Binary (base 2) ฐานสอง | \(1 \times 2^7\) | \(1 \times 2^6\) | \(1 \times 2^5\) | \(1 \times 2^4\) | \(1 \times 2^3\) | \(1 \times 2^2\) | \(1 \times 2^1\) | \(1 \times 2^0\) | 255 |
| Hexadecimal (base 16) ฐานสิบหก | — | — | — | — | — | — | \(15 \times 16^1\) | \(15 \times 16^0\) | 255 |
1 Denary place values 1 ค่าประจำหลักฐานสิบ
In denary, the rightmost digit is ones (\(10^0\)), then tens (\(10^1\)), hundreds (\(10^2\)), and so on.
Use print() to show the expanded sum for 1289.
ในฐานสิบ หลักขวาสุดคือหน่วย (\(10^0\)) แล้วเป็นหลักสิบ (\(10^1\)) หลักร้อย (\(10^2\)) ตามลำดับ
ใช้ print() แสดงการกระจายผลรวมของเลข 1289
1000 200 80 9 1289
2 Binary place values and bin()
2 ค่าประจำหลักฐานสอง และ bin()
Binary place values are powers of 2: … 8, 4, 2, 1.
For 1011 (binary), sum the places where the bit is 1.
bin(n) converts a denary integer to a binary string prefixed with 0b.
ค่าประจำหลักฐานสองเป็นกำลังของ 2 เช่น … 8, 4, 2, 1
สำหรับ 1011 (ฐานสอง) ให้บวกเฉพาะหลักที่บิตเป็น 1
bin(n) แปลงจำนวนเต็มฐานสิบเป็นสตริงฐานสองที่ขึ้นต้นด้วย 0b
11 0b1011 11
3 Hexadecimal digits and hex()
3 หลักฐานสิบหก และ hex()
Hex digits A–F mean 10–15. One byte (0–255) needs at most two hex digits.
hex(n) returns a string with prefix 0x.
Use int("…", 16) to parse a hex digit string back to denary.
หลัก A–F ในฐานสิบหกแทนค่า 10–15 หนึ่งไบต์ (0–255) ใช้เลขฐานสิบหกได้อย่างมาก 2 หลัก
hex(n) คืนสตริงที่ขึ้นต้นด้วย 0x
ใช้ int("…", 16) เพื่อแปลงสตริงฐานสิบหกกลับเป็นฐานสิบ
26 0x1a 26 255
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 place values and bases:
แหล่งที่ใช้ประกอบเนื้อหาค่าประจำหลักและระบบฐาน:
- Numeral systems — Wikipedia — Positional notation / Hexadecimal (bases and place values) (ฐานและค่าประจำหลัก)
-
Python
bin/hex— Built-in Functions — bin / hex (used in the demos) (ใช้ในตัวอย่าง Run)