Bits and binary · บิตและเลขฐานสอง
Bits: the smallest piece of data
- A bit is one digit that is either
0or1. - Computers store everything — numbers, text, images — as bits.
- One bit alone is small, so we group many bits together.
บิต: ส่วนย่อยที่สุดของข้อมูล
- บิต เป็นตัวเลขหนึ่งตำแหน่งที่เป็นeither
0or1 - คอมพิวเตอร์จัดเก็บทุกอย่าง — ตัวเลข, ข้อความ, รูปภาพ — เป็นบิต
- บิตเดียวมีขนาดเล็กมาก所以我们 groupedหลายบิตเข้าด้วยกัน
Why computers use binary
- A computer chip has tiny switches. Each switch is on or off.
on=1andoff=0. So two states fit a chip perfectly.- Counting with only
0and1is called binary (base 2).
ทำไมคอมพิวเตอร์ใช้เลขฐานสอง
- ชิปคอมพิวเตอร์มีสวิตช์ขนาดเล็ก สวิตช์แต่ละตัวเป็น เปิด หรือ ปิด
on=1และoff=0ดังนั้นสองสถานะจึงพอดีกับชิปอย่างสมบูรณ์- การนับโดยใช้เฉพาะ
0and1เรียกว่า เลขฐานสอง (base 2)
How binary counts
- Our normal numbers use base 10 (digits
0–9). - Binary uses base 2. Each place is worth twice the place on its right.
- The places are
8 4 2 1. So1101means8 + 4 + 0 + 1 = 13.
เลขฐานสองนับอย่างไร
- ตัวเลขปกติของเราใช้ base 10 (digits
0–9) - เลขฐานสองใช้ base 2 Each place is worth twice the place on its right
- The places are
8 4 2 1. So1101means8 + 4 + 0 + 1 = 13
place: 8 4 2 1
bits: 1 1 0 1 -> 8 + 4 + 0 + 1 = 13
Python can convert for you
bin(13)gives the string'0b1101'. The0bjust means "this is binary".int("1101", 2)reads"1101"as base 2 and gives13.- These two functions go in opposite directions.
Python สามารถแปลงให้คุณได้
bin(13)ให้ string'0b1101'. The0bjust means "this is binary"int("1101", 2)อ่าน"1101"as base 2 and gives13- ฟังก์ชันทั้งสองนี้ทำงานในทิศทางตรงกันข้าม
print(bin(13)) # decimal -> binary
print(int("1101", 2)) # binary -> decimal
Fixed bits and overflow
- A computer keeps a number in a fixed number of bits.
- With 4 bits you can store
0to15— that is2**4 = 16different values. - If a result is too big, it wraps around back to
0. This is overflow.
บิตคงที่และ overflow
- คอมพิวเตอร์เก็บตัวเลข在一个 fixed number of bits
- With 4 bits you can store
0to15— that is2**4 = 16different values - If a result is too big, it wraps around back to
0. This is overflow
print(2 ** 4) # how many values 4 bits hold = 16
print((15 + 3) % 16) # 18 is too big, so it wraps to 2
Common mistakes
nbits store2**ndifferent values, from0to2**n - 1.- Binary is base 2: each place is worth twice the one to its right.
ข้อผิดพลาดที่พบบ่อย
nbits store2**ndifferent values, from0to2**n - 1- Binary is base 2: each place is worth twice the one to its right
Now you try
- Type your code below and press Run to see the output.
- Press Check answer to test it.
ลองดูเลย
- พิมพ์โค้ดของคุณด้านล่างแล้วกด Run เพื่อดูผลลัพธ์
- กด Check answer เพื่อทดสอบ
Binary counts in powers of 2 · เลขฐานสองนับตามกำลังของ 2
Each bit is a power of two; lit bits add up to the value. · แต่ละบิตคือ กำลังของสอง; บิตที่ติดขึ้นมารวมกันจะได้ค่า
Use bin(...) to print the binary form of 20. The output should look like 0b10100. · ใช้ bin(...) เพื่อพิมพ์รูปแบบเลขฐานสองของ 20 ผลลัพธ์ควรดูเหมือน 0b10100
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The binary string "110" is a number. Use int(s, 2) to print it as a normal (decimal) number. · สตริงเลขฐานสอง "110" คือตัวเลข ใช้ int(s, 2) เพื่อพิมพ์它以十进制形式 (normal decimal number)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
A counter has 5 bits, so it holds 2**5 = 32 values. Store 2**5 in size. Then store (30 + 5) % size in result to show how the value wraps around (overflow). · เคาน์เตอร์มี 5 บิต จึงเก็บค่าได้ถึง 2**5 = 32 เก็บ 2**5 ใน size จากนั้นเก็บ (30 + 5) % size ใน result เพื่อดูว่าค่าวนรอบอย่างไร (overflow)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่