အခန်း 24 · animation 1 ခု

Appendix A: Binary Number များ (Binary Numbers)

808 Coder မှ တင်ဆက်သည်

ဒီအခန်းပြီးရင်...

  • binary ဆိုတာ ဘာလဲ၊ base-2 number system ဆိုတာ ဘယ်လို အလုပ်လုပ်သလဲ ရှင်းပြနိုင်မယ်
  • ကွန်ပျူတာတွေက binary ကို ဘာလို့ သုံးရသလဲ နားလည်မယ်
  • decimal ကို binary ပြောင်းနိုင်မယ် (divide-by-2 method)
  • binary ကို decimal ပြန်ပြောင်းနိုင်မယ် (positional method)
  • Python မှာ bin() နဲ့ int(x, 2) ကို သုံးနိုင်မယ်

Binary ဆိုတာ ဘာလဲ

နေ့တိုင်းသုံးနေတဲ့ ကိန်းတွေက decimal — base-10 number system ပါ။ ဂဏန်း 0 ကနေ 9 အထိ 10 လုံး သုံးပြီး ကိန်းတိုင်းကို ရေးနိုင်တယ်။

Binary ကတော့ base-2 number system ဖြစ်ပါတယ်။ ဂဏန်း 2 လုံးပဲ သုံးတယ် — 0 နဲ့ 1။

binary ရဲ့ ဂဏန်းတစ်လုံးချင်းစီကို bit လို့ ခေါ်တယ်။ bit တစ်ခုက 0 သို့မဟုတ် 1 ဖြစ်နိုင်တယ်၊ ဒါပဲ။

A bit is a switch: ON (1) or OFF (0) 1 ON 0 OFF a single bit 1 1 0 1 four bits: 1 1 0 1 = decimal 13 8 bits = 1 byte -> 256 possible values (0-255)
bit တွေကို ON/OFF switch အဖြစ် ပြခြင်း

ကွန်ပျူတာတွေက ဘာလို့ binary ကို သုံးသလဲ

ကွန်ပျူတာထဲမှာ အရာအားလုံး လျှပ်စစ်နဲ့ အလုပ်လုပ်တယ်။ လျှပ်စစ်မီးလုံးလေးကို စိတ်ကူးကြည့်ပါ — မီးပွင့်နေတယ် (ON) သို့မဟုတ် မီးပိတ်နေတယ် (OFF) ဒီနှစ်ခုပဲ ရှိတယ်။

ကွန်ပျူတာဟာလဲ အတူတူပဲ။ Circuit တစ်ခုချင်းစီမှာ လျှပ်စစ်စီးဆင်းမှု ရှိ သို့မဟုတ် မရှိ ဆိုတဲ့ အခြေအနေ 2 ခုပဲ ရှိပါတယ်။

  • ON = 1
  • OFF = 0

အဲဒါကြောင့် ကွန်ပျူတာတွေက binary (0 နဲ့ 1) ကို သုံးတာပါ။ သင်ရိုက်တဲ့ စာတွေ၊ သင်ကြည့်နေတဲ့ ပုံတွေ၊ Python program တွေအကုန်လုံး — နောက်ကွယ်မှာ 0 နဲ့ 1 တွေနဲ့ပဲ သိမ်းဆည်းပြီး လုပ်ဆောင်နေတာ ဖြစ်တယ်။

မှတ်ချက်: bit 8 လုံးစုပြီး byte လို့ ခေါ်တယ်။ စာတစ်လုံးစာ (ဥပမာ A) ကို သိမ်းဖို့ byte တစ်ခု သုံးတယ်။ Chapter 7 မှာ ord() နဲ့ chr() နဲ့ ချိတ်ဆက်ပြီး ဒီအကြောင်းကို ပြန်တွေ့ရမယ်။

Decimal ကို Binary ပြောင်းခြင်း

decimal number ကို binary ပြောင်းဖို့ အဆင့်တွေက ဒီလိုပါ။

  1. decimal number ကို 2 နဲ့ စားပါ။
  2. Remainder (ကျန်ကျား) ကို ချရေးထားပါ။
  3. Quotient (စားလဒ်) ကို ထပ်ပြီး 2 နဲ့ စားပါ။
  4. Quotient က 0 ဖြစ်သွားတဲ့ အထိ ထပ်လုပ်ပါ။
  5. Remainder တွေကို နောက်ကနေ ရှေ့ကို ပြောင်းပြန် ဖတ်ပါ — အဲဒါ binary number ပဲ။

ဥပမာ: 13 ကို binary ပြောင်းမယ်

အဆင့် တွက်ချက်မှု Quotient Remainder
1 13 ÷ 2 6 1
2 6 ÷ 2 3 0
3 3 ÷ 2 1 1
4 1 ÷ 2 0 1

Remainder တွေကို ပြောင်းပြန် ဖတ်ပါ: 1 1 0 1

ဒါကြောင့် 13 = 1101 (binary) ပါ။

Python မှာ ဒီအဆင့်တွေကို while loop နဲ့ ရေးနိုင်တယ် — Chapter 5 မှာ ဒီလိုမျိုး loop example ကို လေ့လာဖူးပြီလေ။

number = 13
binary = ""

while number > 0:
    remainder = number % 2
    binary = str(remainder) + binary
    number = number // 2

print(binary)

Output:

1101

ဒါ့အပြင် Python မှာ ready-made function လဲ ရှိတယ် — bin():

print(bin(13))
print(bin(21))

Output:

0b1101
0b10101

မှတ်ချက်: bin() ရဲ့ အဖြေမှာ ရှေ့မှာ 0b ဆိုတဲ့ prefix ပါတယ်။ ဒါက "ဒီကိန်းက binary ပါ" လို့ Python ကို ပြောပြတဲ့ သင်္ကေတပါ။ [2:] နဲ့ ဖြတ်ထုတ်နိုင်တယ်: bin(13)[2:] → '1101'။

Binary ကို Decimal ပြန်ပြောင်းခြင်း

binary ကို decimal ပြန်ပြောင်းဖို့ positional value method သုံးတယ်။ အဆင့်တွေက:

  1. Binary number ကို ချရေးပါ။
  2. bit တစ်ခုချင်းစီကို 2 ရဲ့ power နဲ့ မြှောက်ပါ — ညာဘက်ဆုံးကနေ စပြီး position 0, 1, 2, 3...။
  3. အားလုံးကို ပေါင်းပါ။

ဥပမာ: 1101 ကို decimal ပြောင်းမယ်

     1      1      0      1
     ↓      ↓      ↓      ↓
   2³     2²     2¹     2⁰
   = 8    = 4    = 2    = 1

1 × 8 + 1 × 4 + 0 × 2 + 1 × 1 = 8 + 4 + 0 + 1 = 13

ဒါကြောင့် 1101 (binary) = 13 (decimal) ပါ။

မှတ်ထားဖို့ လွယ်တဲ့ နည်းက 2 ရဲ့ power တွေကို မှတ်ထားဖို့ပဲ: 1, 2, 4, 8, 16, 32, 64, 128... ညာကနေ ဘယ်ကို တစ်ဆင့်တက်ရင် နှစ်ဆ ဖြစ်သွားတယ်။

Python မှာ int() ကို base-2 နဲ့ သုံးပြီး ပြောင်းနိုင်တယ်:

print(int("1101", 2))
print(int("10101", 2))

Output:

13
21

သတိပြုရန်: int("1101", 2) မှာ binary ကို string "1101" အနေနဲ့ ပေးရမယ်။ int(1101, 2) လို့ number အနေနဲ့ ပေးရင် error တက်မယ်။

0 ကနေ 15 အထိ Binary Table

Decimal Binary Decimal Binary
0 0 8 1000
1 1 9 1001
2 10 10 1010
3 11 11 1011
4 100 12 1100
5 101 13 1101
6 110 14 1110
7 111 15 1111

Pattern ကို သတိထားမိလား? Binary ကို တစ်တိုးရင် နောက်ဆုံး bit က 0 နဲ့ 1 ကြား လှည့်ပတ်နေတယ် — decimal ရဲ့ odd/even လိုပဲ။ 3-bit (111) နဲ့ 4-bit (1000) ကို ဖြတ်ကျော်သွားတဲ့အခါ bit တစ်လုံး ထပ်တိုးလာတယ်။

Python နဲ့ table ကို ကိုယ်တိုင် ထုတ်ကြည့်နိုင်တယ်:

for i in range(16):
    print(i, "->", bin(i)[2:])

Output:

0 -> 0
1 -> 1
2 -> 10
3 -> 11
4 -> 100
5 -> 101
6 -> 110
7 -> 111
8 -> 1000
9 -> 1001
10 -> 1010
11 -> 1011
12 -> 1100
13 -> 1101
14 -> 1110
15 -> 1111

မှတ်ချက်: Chapter 7 မှာ ord('A') က 65 ဖြစ်ပြီး chr(65) က 'A' ဖြစ်တယ်ဆိုတာ လေ့လာရမယ်။ အဲဒီ 65 ကို binary ပြောင်းကြည့်ပါ — bin(65)[2:] → '1000001'။ ကွန်ပျူတာထဲမှာ စာလုံး A က တကယ်တော့ ဒီ bit တွေနဲ့ သိမ်းထားတာပါ။


လေ့ကျင့်ခန်းများ

1. Decimal 21 ကို binary ပြောင်းပါ။ (အဖြေပါ)

21 ÷ 2 = 10 remainder 1; 10 ÷ 2 = 5 remainder 0; 5 ÷ 2 = 2 remainder 1; 2 ÷ 2 = 1 remainder 0; 1 ÷ 2 = 0 remainder 1။ Remainder တွေကို ပြောင်းပြန်ဖတ်ရင် 10101။

အမှန် စစ်ဆေးရန်: print(bin(21)) → 0b10101။

2. Binary 10110 ကို decimal ပြောင်းပါ။ (အဖြေပါ)

1×16 + 0×8 + 1×4 + 1×2 + 0×1 = 16 + 0 + 4 + 2 + 0 = 22။

အမှန် စစ်ဆေးရန်: print(int("10110", 2)) → 22။

3. Decimal 42 ကို binary ပြောင်းပြီး bin() နဲ့ အဖြေတိုက်ကြည့်ပါ။

Hint: 42 က 32 + 8 + 2 ပါ — binary မှာ 1 တွေက ဘယ်နေရာတွေမှာ ရှိမလဲ?

4. Binary 11100 ကို decimal ပြောင်းပါ။ ပြီးရင် int("11100", 2) နဲ့ တိုက်ကြည့်ပါ။

5. Program ရေးပါ: user ဆီက decimal number တစ်ခု ယူပြီး binary ထုတ်ပေးတဲ့ program ကို while loop နဲ့ ရေးပါ။ (ဒီအခန်းရဲ့ decimal-to-binary loop ကို ပြန်သုံးပါ။)