Data Representation — Introduction การแทนข้อมูล — บทนำ
Bits, bytes, and why computers store everything as 0 and 1 บิต ไบต์ และเหตุผลที่คอมพิวเตอร์เก็บทุกอย่างเป็น 0 กับ 1
Inside a computer, data is stored as bits. Each bit can be only 0 or 1.
Numbers, text, images, and sound must first be converted into bits before the machine can store or process them —
for example, the letter A can be represented as the bit pattern 01100001.
This intro sets the foundation; later lessons cover
number systems,
conversion,
negatives,
binary arithmetic, and
ASCII.
คอมพิวเตอร์เก็บข้อมูลทุกชนิดในรูปแบบ บิต (bit) ซึ่งมีค่าได้เพียง 0 หรือ 1 เท่านั้น
ดังนั้นตัวเลข ข้อความ ภาพ และเสียง ต้องถูกแปลงให้อยู่ในรูปบิตก่อน จึงจะเก็บหรือประมวลผลได้
เช่น ตัวอักษร A สามารถแปลงเป็น bit ได้เป็น 01100001 เป็นต้น
บทนี้ปูแนวคิดพื้นฐาน ส่วนรายละเอียด
ระบบเลขฐาน
การแปลง
จำนวนลบ
การคำนวณฐานสอง และ
ASCII จะอยู่ในบทถัดไป
1 A bit is only 0 or 1 1 บิตมีค่าได้แค่ 0 หรือ 1
Use print() to show the two possible bit values. A bit cannot be 2, 5, or any other digit.
ใช้ print() แสดงค่าที่เป็นไปได้ของบิต มีเพียงสองค่าเท่านั้น — ไม่สามารถเป็น 2, 5 หรือหลักอื่นได้
0 1
2 One byte has 8 bits 2 หนึ่งไบต์มี 8 บิต
A byte is a group of exactly 8 bits. Here we store eight bit values in a list and use
len() to count how many bits are in one byte.
ไบต์ (byte) คือกลุ่มบิตที่มีความยาวคงที่ 8 บิต ตัวอย่างนี้เก็บค่าบิต 8 ค่าใน list
แล้วใช้ len() นับจำนวนบิตในหนึ่งไบต์
[1, 0, 0, 1, 0, 1, 1, 0] 8
3 Why convert data into bits? 3 ทำไมต้องแปลงข้อมูลเป็นบิต?
Different kinds of information (numbers, text, images, audio) all become sequences of bits
before storage — for example, numeric strings and text in kinds = ["10", "abcd"].
This demo converts them to bits using real Python bin().
ข้อมูลแต่ละชนิด (ตัวเลข ข้อความ ภาพ เสียง) ต้องกลายเป็นลำดับของบิตก่อนเก็บ
เช่น ข้อความ และตัวเลข kinds = ["10", "abcd"]
Use bin(int(...)) for numeric strings and bin(ord(ch)) for each character. the output result is ob[binary number]
ใช้ bin(int(...)) แปลงตัวเลข และ bin(ord(ch)) แปลงตัวอักษรเป็นบิต เมื่อผลที่ได้คือ ob[เลข binary]
10 0b1010 a 0b1100001 b 0b1100010 c 0b1100011 d 0b1100100
4 How many values fit in n bits? 4 n บิตเก็บได้กี่ค่า?
Every extra bit doubles the number of patterns: n bits give 2 ** n different values.
That is why one colour channel of a pixel uses 8 bits (256 levels, 0–255), and why the original ASCII table
used 7 bits (128 characters). The operator ** means “to the power of”, so
2 ** 3 is 2 × 2 × 2 = 8.
บิตที่เพิ่มขึ้นทุก 1 บิตทำให้จำนวน pattern เพิ่มเป็นสองเท่า: n บิตแทนได้ 2 ** n ค่า
นี่คือเหตุผลที่สีหนึ่งช่องของ pixel ใช้ 8 บิต (256 ระดับ คือ 0–255) และตาราง ASCII ดั้งเดิมใช้ 7 บิต (128 ตัวอักษร)
ตัวดำเนินการ ** หมายถึง “ยกกำลัง” เช่น 2 ** 3 คือ 2 × 2 × 2 = 8
Notice how slowly the number of bits grows (1, 4, 8, 16) compared with how fast the number of values grows — 16 bits already give 65,536 different values.
สังเกตว่าจำนวนบิต (1, 4, 8, 16) เพิ่มขึ้นช้า แต่จำนวนค่าที่แทนได้เพิ่มเร็วมาก เพียง 16 บิตก็แทนได้ถึง 65,536 ค่าแล้ว
1 bits -> 2 values 4 bits -> 16 values 8 bits -> 256 values 16 bits -> 65536 values
5 Common mistakes 5 ข้อผิดพลาดที่พบบ่อย
-
Mixing up bit and byte. A lowercase
bis a bit and an uppercaseBis a byte; one byte is 8 bits, so a 100 Mb/s link moves about 12.5 MB per second, not 100. -
สับสนระหว่าง bit กับ byte ตัวพิมพ์เล็ก
bคือบิต ตัวพิมพ์ใหญ่Bคือไบต์ 1 ไบต์มี 8 บิต ดังนั้นเน็ต 100 Mb/s ส่งข้อมูลได้ราว 12.5 MB ต่อวินาที -
Thinking
bin()returns a number. It returns a string such as'0b101'; the prefix0bonly says “this is binary” and is not part of the value. The demo below shows the value, its type, and its length. -
คิดว่า
bin()ให้ตัวเลข จริง ๆ แล้วได้ สตริง เช่น'0b101'ส่วนนำหน้า0bแค่บอกว่านี่คือเลขฐานสอง ไม่ใช่ส่วนของค่า ตัวอย่างด้านล่างแสดงค่า ชนิดข้อมูล และความยาวของผลลัพธ์ -
Forgetting that a bit pattern has no meaning on its own.
01100001is the letteraonly because ASCII / Unicode says so; read as a number it is 97. Every kind of data needs an agreed table or rule that tells the computer how to interpret the bits. -
อย่าลืมว่าบิตแต่ละชุดไม่มีความหมายในตัวเอง
01100001เป็นตัวอักษรaเพราะ ASCII / Unicode กำหนดไว้ แต่ถ้าอ่านเป็นตัวเลขจะได้ 97 ข้อมูลทุกชนิดต้องมีตารางหรือกติกาที่ตกลงกัน เพื่อบอกคอมพิวเตอร์ว่าจะตีความบิตอย่างไร
0b101 <class 'str'> 5
6 Think further 6 แบบฝึกคิดต่อ
Try these on paper first, then check them with the Run boxes above:
ลองคิดบนกระดาษก่อน แล้วค่อยตรวจด้วยกล่อง Run ด้านบน:
- How many different values can one byte hold, and what is the largest whole number it can store (no negatives)? Hint: 2 ** 8 − 1.
- 1 ไบต์เก็บได้กี่ค่า และเก็บจำนวนเต็มที่มากที่สุดได้เท่าไร (ไม่รวมจำนวนลบ) Hint: 2 ** 8 − 1
-
What is the smallest number of bits needed to tell apart 100 different states, such as 100 brightness levels?
Hint: find the smallest
nwhere2 ** nis at least 100. -
ต้องใช้อย่างน้อยกี่บิตเพื่อแยก 100 สถานะ เช่น ความสว่าง 100 ระดับ
Hint: หา
nที่น้อยที่สุดที่2 ** nมากกว่าหรือเท่ากับ 100 -
Why do
Aandahave different bit patterns? Hint: comparebin(ord("A"))withbin(ord("a")), then read the ASCII Table lesson. -
ทำไม
Aกับaจึงมี bit pattern ต่างกัน Hint: เทียบbin(ord("A"))กับbin(ord("a"))แล้วอ่านบท ตาราง ASCII
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 this introduction:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
- Bits and bytes (overview) — Wikipedia — Bit / Byte (definitions of bit and byte) (นิยามบิตและไบต์)
-
Python
print/len— Built-in Functions — print / len (used in the demos) (ใช้ในตัวอย่าง Run)