Decimal · base 10
42 = 4 × 10¹ + 2 × 10⁰
See the place. Follow the steps. Explain the code.
42₁₀ = 101010₂ = 2A₁₆. The subscripts name the base; they are not part of the number. This lesson covers unsigned whole numbers. Negative numbers, two’s complement and fractions need additional rules.
In base b, the rightmost place is b⁰ = 1. Moving one place left multiplies the weight by b. A digit contributes digit × weight; add all contributions.
42 = 4 × 10¹ + 2 × 10⁰
101010₂ = 32 + 8 + 2 = 42₁₀
2A₁₆ = 2 × 16¹ + 10 × 16⁰ = 42₁₀
Binary digits are 0–1. Decimal digits are 0–9. Hexadecimal digits are 0–9 and A–F, where A=10, B=11, C=12, D=13, E=14 and F=15. The largest digit is always base − 1.
Choose a method and enter a value. Predict each next step before revealing it. Play advances once every 1.2 seconds; Pause, Previous, Next and Reset give you control. Nothing plays automatically.
101010₂: 1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 42₁₀Write weights from the right: 1, 2, 4, 8, 16, 32. Keep zero columns so the other digits stay aligned.
42 ÷ 2 = 21 r0
21 ÷ 2 = 10 r1
10 ÷ 2 = 5 r0
5 ÷ 2 = 2 r1
2 ÷ 2 = 1 r0
1 ÷ 2 = 0 r1
42₁₀ = 101010₂Divide the quotient again until it is zero. Read remainders from bottom to top: the first remainder is the rightmost digit. For zero, write 0.
FF₁₆ = 15×16 + 15 = 255₁₀
255 ÷ 16 = 15 r15 (F)
15 ÷ 16 = 0 r15 (F)
255₁₀ = FF₁₆Use powers of 16 to decode hexadecimal. To encode decimal, divide repeatedly by 16 and reverse the remainders; replace 10–15 with A–F.
101010₂ → 0010 1010 → 2 A → 2A₁₆
2A₁₆ → 0010 1010₂ → 101010₂Because 16 = 2⁴, one hex digit matches exactly four bits. Group binary from the right; pad only the leftmost group with zeros. Expand every hex digit to four bits, including inner zero groups.
These are worked Python 3 examples with exact output. Copy them into the existing Python playground, predict the result, then run them.
Open Python playground →n = 42
print(bin(n), hex(n))
print(format(n, "08b"), format(n, "02X"))
print(int("101010", 2), int("2A", 16))0b101010 0x2a
00101010 2A
42 42bin() and hex() return strings with prefixes. format() can omit the prefix, use uppercase hex, and pad to a minimum width. int(text, base) parses a string into an integer. Padding does not change the value or impose an 8-bit limit.
def to_base(n, base):
if n < 0 or base not in (2, 16):
raise ValueError("Use non-negative integers and base 2 or 16")
if n == 0:
return "0"
digits = "0123456789ABCDEF"
result = ""
while n > 0:
n, remainder = divmod(n, base)
result = digits[remainder] + result
return result
for n in [0, 10, 15, 16, 42, 255]:
print(n, to_base(n, 2), to_base(n, 16))0 0 0
10 1010 A
15 1111 F
16 10000 10
42 101010 2A
255 11111111 FFThe function expects integer arguments. divmod() gives the quotient and remainder. Prepending each digit reverses the remainder order. The zero case prevents an empty result. Trace n=16: hexadecimal carries into a new place as 10, while binary becomes 10000.
10000001₂ = 128 + 1 = 129₁₀; 3F₁₆ = 3 × 16 + 15 = 63₁₀.