Number Systems: Binary, Octal, Hex
Computer binary क्यों use करते हैं?
Computer के अंदर हर circuit एक छोटे switch जैसा है जो या तो ON है या OFF — current बहता है या नहीं। दो states बनाना आसान और बहुत भरोसेमंद है, इसलिए computer हर चीज़ दो digits से दिखाता है: 1 (ON) और 0 (OFF)। यही binary number system है। Numbers, letters, photos, music और Python programs भी आख़िर में 0 और 1 के patterns के रूप में store होते हैं।
Base (radix) और place value
किसी number system का base (या radix) उसमें use होने वाले अलग digits की संख्या है। हर positional system में digit की value उसकी position पर निर्भर करती है। Positions दाईं ओर से 0, 1, 2 … गिनी जाती हैं और हर position का weight baseposition होता है।
| System | Base | Digits | Example |
|---|---|---|---|
| Decimal | 10 | 0 1 2 3 4 5 6 7 8 9 | (725)₁₀ |
| Binary | 2 | 0 1 | (1011)₂ |
| Octal | 8 | 0 1 2 3 4 5 6 7 | (375)₈ |
| Hexadecimal | 16 | 0–9 और A B C D E F (A=10 … F=15) | (2AF)₁₆ |
Decimal (base 10)
यह वह system है जो हम रोज़ use करते हैं। 725 में: 7 सैकड़े (hundreds) पर, 2 दहाई (tens) पर और 5 इकाई (ones) पर है।
| Digit | Position | Weight | Value |
|---|---|---|---|
| 7 | 2 | 10² = 100 | 700 |
| 2 | 1 | 10¹ = 10 | 20 |
| 5 | 0 | 10⁰ = 1 | 5 |
Binary (base 2)
सिर्फ़ digits 0 और 1 allowed हैं। हर binary digit एक bit है। Place values दाएँ से बाएँ दोगुनी होती हैं: 1, 2, 4, 8, 16, 32, 64, 128 …
सबसे दाईं bit Least Significant Bit (LSB) है; सबसे बाईं Most Significant Bit (MSB)। 0 पर ख़त्म होने वाला binary number even है; 1 पर ख़त्म होने वाला odd।
Octal (base 8)
Octal में digits 0 से 7 हैं। Weights 1, 8, 64, 512 … हैं। क्योंकि 8 = 2³, एक octal digit = ठीक 3 bits। Octal पुराने computers में popular था और Linux में file permissions के लिए आज भी use होता है, जैसे chmod 755।
Example: (57)₈ = 5 × 8 + 7 × 1 = 40 + 7 = decimal में 47।
Hexadecimal (base 16)
Hexadecimal को 16 symbols चाहिए, इसलिए 9 के बाद letters use करते हैं: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15। क्योंकि 16 = 2⁴, एक hex digit = ठीक 4 bits। Weights 1, 16, 256, 4096 … हैं।
Example: (2F)₁₆ = 2 × 16 + 15 × 1 = 32 + 15 = 47।
Comparison table 0–16
Table रटने की जगह Python से print कराएँ। देखिए hex 15 पर F तक पहुँचता है, और octal 8 पर "carry" होकर 10 बनता है।
print('Dec Bin Oct Hex')
for n in range(0, 17):
print(f'{n:>3} {n:>04b} {n:>3o} {n:>3X}')0–15 का binary (4-bit patterns) याद कर लें। इन 16 patterns से आप कोई भी binary ↔ hex तुरंत बदल सकते हैं, और पहले 8 (3-bit) patterns से binary ↔ octal।
Python में number systems
Python में दूसरे bases में number देखने के built-in functions हैं: bin(), oct(), hex()। Prefix 0b, 0o, 0x बताते हैं कि base कौन सा है। int(text, base) वापस decimal में बदलता है।
n = 45
print(bin(n)) # binary
print(oct(n)) # octal
print(hex(n)) # hexadecimal
print(int('101101', 2), int('55', 8), int('2D', 16))आप सीधे भी इन bases में numbers लिख सकते हैं: 0b1010 = 10, 0o17 = 15 और 0xFF = 255।
सारांश और practice
- Base = digits की संख्या: decimal 10, binary 2, octal 8, hexadecimal 16।
- Value = Σ digit × baseposition, position दाईं ओर से 0 से।
- 1 octal digit = 3 bits; 1 hex digit = 4 bits।
- Python:
bin(),oct(),hex(),int(s, base)।
1) इनमें से कौन valid octal number नहीं है: 175, 382, 66? 2) (1F)₁₆ की decimal value क्या है? 3) Hex number 3A9 लिखने में कितने bits लगेंगे?
Answers: 1) 382 (digit 8 allowed नहीं)। 2) 31। 3) 3 hex digits × 4 = 12 bits।
