Number System Conversions
Conversion map
बस दो बड़े तरीके हैं। Decimal से किसी दूसरे base में जाने के लिए बार-बार divide करें। Decimal में आने के लिए place values से multiply करके जोड़ें। Binary, octal और hex के बीच तेज़ grouping shortcuts हैं।
Decimal → binary, octal या hexadecimal
Rule: decimal number को नए base से divide करें, remainder लिखें, quotient को फिर divide करें, और quotient 0 होने तक चलते रहें। Remainders नीचे से ऊपर पढ़ें।
Example 1: (156)₁₀ → binary
| Divide | Quotient | Remainder |
|---|---|---|
| 156 ÷ 2 | 78 | 0 |
| 78 ÷ 2 | 39 | 0 |
| 39 ÷ 2 | 19 | 1 |
| 19 ÷ 2 | 9 | 1 |
| 9 ÷ 2 | 4 | 1 |
| 4 ÷ 2 | 2 | 0 |
| 2 ÷ 2 | 1 | 0 |
| 1 ÷ 2 | 0 | 1 |
Example 2: (156)₁₀ → octal
156 ÷ 8 = 19 remainder 4; 19 ÷ 8 = 2 remainder 3; 2 ÷ 8 = 0 remainder 2। ऊपर की ओर: (234)₈।
Example 3: (156)₁₀ → hexadecimal
156 ÷ 16 = 9 remainder 12 = C; 9 ÷ 16 = 0 remainder 9। ऊपर की ओर: (9C)₁₆।
Hex में 10–15 का remainder letter A–F के रूप में लिखें, दो digits में नहीं। 156 को "912" लिखना ग़लत है; सही 9C है।
किसी भी base → decimal
Rule: हर digit को baseposition (दाईं ओर position 0) से multiply करें और सब जोड़ दें।
| Number | Expansion | Decimal |
|---|---|---|
| (10011100)₂ | 1×128 + 0×64 + 0×32 + 1×16 + 1×8 + 1×4 + 0×2 + 0×1 | 156 |
| (234)₈ | 2×64 + 3×8 + 4×1 | 156 |
| (9C)₁₆ | 9×16 + 12×1 | 156 |
| (1011.1)₂ | 8 + 0 + 2 + 1 + 1×2⁻¹ | 11.5 |
Point के बाद वाले digits की positions negative होती हैं: −1, −2 … इसलिए binary में weights ½ = 0.5, ¼ = 0.25, ⅛ = 0.125 … हैं।
Decimal fractions → binary
Rule: fractional part को 2 से multiply करें। Integer part (0 या 1) अगली bit है। सिर्फ़ fraction रखें और दोहराएँ। Bits ऊपर से नीचे पढ़ें।
| Step | Multiply | Result | Bit |
|---|---|---|---|
| 1 | 0.625 × 2 | 1.25 | 1 |
| 2 | 0.25 × 2 | 0.5 | 0 |
| 3 | 0.5 × 2 | 1.0 | 1 |
| 4 | 0.0 – stop |
x = 0.625
bits = ''
for step in range(4):
x = x * 2
bits += str(int(x)) # integer part is the next bit
x = x - int(x)
print('0.' + bits)इसलिए (12.625)₁₀ = (1100.101)₂ — integer part को division से और fraction को multiplication से बदलें, फिर दोनों जोड़ दें।
Binary ↔ octal (3 के groups)
Binary → octal: दाईं ओर से 3-3 bits के groups बनाएँ (ज़रूरत हो तो बाईं ओर 0 जोड़ें) और हर group की value लिखें।
Octal → binary: हर octal digit को 3 bits में लिखें। (57)₈ → 101 111 → (101111)₂।
Binary ↔ hexadecimal (4 के groups)
Binary → hex: दाईं ओर से 4 bits के groups। (10011100)₂ → 1001 1100 → 9 C → (9C)₁₆।
Hex → binary: हर hex digit 4 bits बनता है। (A7)₁₆ → 1010 0111 → (10100111)₂।
Octal ↔ hexadecimal
सीधा shortcut नहीं है, इसलिए बीच में binary use करें।
उल्टा example: (B2)₁₆ → 1011 0010 → 3 के groups: 010 110 010 → (262)₈।
Python में अपना converter
यह program वही repeated-division तरीका use करता है जो आप paper पर करते हैं — % remainder देता है, // quotient।
def to_base(n, base):
digits = '0123456789ABCDEF'
if n == 0:
return '0'
result = ''
while n > 0:
result = digits[n % base] + result # remainder becomes next digit
n = n // base
return result
for b in (2, 8, 16):
print(156, 'in base', b, '=', to_base(156, b))और उल्टा, place-value तरीके से (Horner's तरीका: चल रही value को base से multiply करके अगला digit जोड़ें):
def to_decimal(text, base):
digits = '0123456789ABCDEF'
value = 0
for ch in text.upper():
value = value * base + digits.index(ch)
return value
print(to_decimal('10011100', 2))
print(to_decimal('234', 8))
print(to_decimal('9C', 16))सारांश और practice
- Decimal → base b: b से divide, remainders ऊपर की ओर।
- Base b → decimal: Σ digit × bposition।
- Fraction → binary: 2 से multiply, integer parts नीचे की ओर।
- Binary ↔ octal: 3 के groups; binary ↔ hex: 4 के groups; octal ↔ hex binary से होकर।
1) (99)₁₀ = (?)₂ 2) (110110)₂ = (?)₁₆ 3) (725)₈ = (?)₁₀ 4) (3F)₁₆ = (?)₈ 5) (0.75)₁₀ = (?)₂
Answers: 1) 1100011 2) 36 3) 469 4) 77 5) 0.11
