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 หรือหลักอื่นได้

[1]
[output]
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() นับจำนวนบิตในหนึ่งไบต์

[2]
[output]
[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]

[3]
[output]
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 ค่าแล้ว

[4]
[output]
1 bits -> 2 values
4 bits -> 16 values
8 bits -> 256 values
16 bits -> 65536 values

5 Common mistakes 5 ข้อผิดพลาดที่พบบ่อย

  1. Mixing up bit and byte. A lowercase b is a bit and an uppercase B is a byte; one byte is 8 bits, so a 100 Mb/s link moves about 12.5 MB per second, not 100.
  2. สับสนระหว่าง bit กับ byte ตัวพิมพ์เล็ก b คือบิต ตัวพิมพ์ใหญ่ B คือไบต์ 1 ไบต์มี 8 บิต ดังนั้นเน็ต 100 Mb/s ส่งข้อมูลได้ราว 12.5 MB ต่อวินาที
  3. Thinking bin() returns a number. It returns a string such as '0b101'; the prefix 0b only says “this is binary” and is not part of the value. The demo below shows the value, its type, and its length.
  4. คิดว่า bin() ให้ตัวเลข จริง ๆ แล้วได้ สตริง เช่น '0b101' ส่วนนำหน้า 0b แค่บอกว่านี่คือเลขฐานสอง ไม่ใช่ส่วนของค่า ตัวอย่างด้านล่างแสดงค่า ชนิดข้อมูล และความยาวของผลลัพธ์
  5. Forgetting that a bit pattern has no meaning on its own. 01100001 is the letter a only 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.
  6. อย่าลืมว่าบิตแต่ละชุดไม่มีความหมายในตัวเอง 01100001 เป็นตัวอักษร a เพราะ ASCII / Unicode กำหนดไว้ แต่ถ้าอ่านเป็นตัวเลขจะได้ 97 ข้อมูลทุกชนิดต้องมีตารางหรือกติกาที่ตกลงกัน เพื่อบอกคอมพิวเตอร์ว่าจะตีความบิตอย่างไร
[5]
[output]
0b101
<class 'str'>
5

6 Think further 6 แบบฝึกคิดต่อ

Try these on paper first, then check them with the Run boxes above:

ลองคิดบนกระดาษก่อน แล้วค่อยตรวจด้วยกล่อง Run ด้านบน:

  1. How many different values can one byte hold, and what is the largest whole number it can store (no negatives)? Hint: 2 ** 8 − 1.
  2. 1 ไบต์เก็บได้กี่ค่า และเก็บจำนวนเต็มที่มากที่สุดได้เท่าไร (ไม่รวมจำนวนลบ) Hint: 2 ** 8 − 1
  3. What is the smallest number of bits needed to tell apart 100 different states, such as 100 brightness levels? Hint: find the smallest n where 2 ** n is at least 100.
  4. ต้องใช้อย่างน้อยกี่บิตเพื่อแยก 100 สถานะ เช่น ความสว่าง 100 ระดับ Hint: หา n ที่น้อยที่สุดที่ 2 ** n มากกว่าหรือเท่ากับ 100
  5. Why do A and a have different bit patterns? Hint: compare bin(ord("A")) with bin(ord("a")), then read the ASCII Table lesson.
  6. ทำไม 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:

แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:

  1. Bits and bytes (overview) — Wikipedia — Bit / Byte (definitions of bit and byte) (นิยามบิตและไบต์)
  2. Python print / len — Built-in Functions — print / len (used in the demos) (ใช้ในตัวอย่าง Run)

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 5

ข้อ 1

1 / 5

How many possible values can one bit have in a computer?บิต (bit) ในคอมพิวเตอร์มีค่าที่เป็นไปได้กี่ค่า?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 2 values (0 and 1)2 ค่า (0 และ 1)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 2 values (0 and 1)2 ค่า (0 และ 1)
    Correct! A bit can only be 0 or 1.ถูกต้อง! บิตมีได้เพียง 0 หรือ 1
  • (B) 10 values (0 through 9)10 ค่า (0 ถึง 9)
    Wrong: ten digits belong to denary, not a single bit.ผิด: 10 ค่าเป็นระบบฐานสิบ ไม่ใช่บิต
  • (C) 8 values8 ค่า
    Wrong: 8 is bits per byte, not values of one bit.ผิด: 8 คือจำนวนบิตในหนึ่งไบต์ ไม่ใช่จำนวนค่าของบิตหนึ่งตัว
  • (D) 16 values16 ค่า
    Wrong: 16 relates to hexadecimal, not one bit.ผิด: 16 เกี่ยวข้องกับฐานสิบหก ไม่ใช่ค่าของบิตเดียว

ข้อ 2

2 / 5

How many bits are in one byte?หนึ่งไบต์ (byte) มีกี่บิต?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) 8 bits8 บิต

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 4 bits4 บิต
    Wrong: a standard byte is not 4 bits.ผิด: 4 บิตไม่ใช่คำนิยามมาตรฐานของหนึ่งไบต์
  • (B) 8 bits8 บิต
    Correct! One byte has exactly 8 bits, as in this lesson.ถูกต้อง! หนึ่งไบต์มี 8 บิตเสมอตามที่อธิบายในบท
  • (C) 10 bits10 บิต
    Wrong: that is not the size of a byte.ผิด: ไม่ใช่จำนวนบิตของหนึ่งไบต์
  • (D) 16 bits16 บิต
    Wrong: 16 bits is not one byte.ผิด: 16 บิตมักเรียกว่า word ในบางระบบ ไม่ใช่หนึ่งไบต์

ข้อ 3

3 / 5

Why must numbers, text, images, and audio be converted into bits first?ทำไมตัวเลข ข้อความ ภาพ และเสียงต้องถูกแปลงเป็นบิตก่อน?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) Because computers store and process data as bitsเพราะคอมพิวเตอร์เก็บและประมวลผลข้อมูลในรูปแบบบิต

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) So they look prettier on screenเพื่อให้พิมพ์สวยขึ้นบนจอ
    Wrong: display beauty is not the reason.ผิด: เหตุผลหลักไม่ใช่ความสวยของหน้าจอ
  • (B) So they work only with Thai textเพื่อให้ใช้ได้เฉพาะภาษาไทย
    Wrong: bit storage is not limited to Thai.ผิด: การแปลงเป็นบิตไม่จำกัดเฉพาะภาษาไทย
  • (C) Because computers store and process data as bitsเพราะคอมพิวเตอร์เก็บและประมวลผลข้อมูลในรูปแบบบิต
    Correct! Computers store and process data as bits.ถูกต้อง! คอมพิวเตอร์ทำงานกับบิต จึงต้องแปลงข้อมูลก่อน
  • (D) Because a bit can be 0 through 9เพราะบิตมีค่าได้ 0 ถึง 9
    Wrong: a bit is only 0 or 1.ผิด: บิตมีได้แค่ 0 กับ 1

ข้อ 4

4 / 5

Which of the following is a valid value for a single bit?ค่าใดต่อไปนี้เป็นค่าที่เป็นไปได้ของบิตหนึ่งตัว?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) 11

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 22
    Wrong: a bit cannot be 2.ผิด: บิตมีได้แค่ 0 หรือ 1
  • (B) 11
    Correct! 1 is a valid bit value.ถูกต้อง! 1 เป็นค่าบิตที่ถูกต้อง
  • (C) 99
    Wrong: 9 is a denary digit, not a bit.ผิด: 9 เป็นหลักในฐานสิบ ไม่ใช่ค่าบิต
  • (D) AA
    Wrong: A is a hex digit, not a single bit value.ผิด: A ใช้ในฐานสิบหก ไม่ใช่ค่าบิตเดียว

ข้อ 5

5 / 5

In the demo with bit_off = 0 and bit_on = 1, what do these values represent?ในตัวอย่าง bit_off = 0 และ bit_on = 1 ค่าเหล่านี้แสดงถึงอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) The two possible values of a single bitค่าที่เป็นไปได้สองค่าของบิตหนึ่งตัว

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) File size in megabytesขนาดของไฟล์เป็นเมกะไบต์
    Wrong: the demo showed bit values, not file size.ผิด: ตัวอย่างนั้นแสดงค่าบิต ไม่ใช่ขนาดไฟล์
  • (B) A pixel color codeรหัสสีของพิกเซล
    Wrong: pixel colors are not covered in this intro.ผิด: ยังไม่ได้สอนเรื่องสีพิกเซลในบทนี้
  • (C) The two possible values of a single bitค่าที่เป็นไปได้สองค่าของบิตหนึ่งตัว
    Correct! It shows a bit is only 0 or 1.ถูกต้อง! แสดงว่าบิตเป็นได้แค่ 0 หรือ 1
  • (D) How many bytes are in one kilobyteจำนวนไบต์ในหนึ่งกิโลไบต์
    Wrong: that demo was not about kilobytes.ผิด: ไม่ได้เกี่ยวกับการนับไบต์ในกิโลไบต์