Number bases (binary & hex)
easy 14 minEvery number base works the same way: each digit is multiplied by the base raised to the power of its position.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What is decimal 10 in binary?
What is 0xFF in decimal?
How many binary bits does one hex digit represent?
What is binary 101010 in decimal?
Converting 13 to binary by division gives remainders 1, 0, 1, 1 in that order. What is 13 in binary?
What is binary 11010110 in hex?
In the number 2A (hex), what is the A worth?
How many bits are needed to store the number 1,000?
What does int("10", 16) return in Python?
Which of these is true for every base b?
Pick an answer to see why.
Not quite. 10 = 8 + 2, so the 8 and 2 places are on: 1010.
Not quite. 0xFF = 15 × 16 + 15 = 240 + 15 = 255, the largest value of one byte.
Not quite. 16 = 2⁴, so one hex digit is exactly 4 bits, a nibble.
Not quite. The on bits are worth 32, 8 and 2: 32 + 8 + 2 = 42.
Not quite. The first remainder is the lowest bit, so read them in reverse: 1101 = 8 + 4 + 1 = 13.
Not quite. Split into nibbles from the right: 1101 = D and 0110 = 6, so 0xD6.
Not quite. A is in position 0, worth 16⁰ = 1, and A = 10, so it contributes 10. The 2 in position 1 contributes 2 × 16 = 32.
Not quite. 2⁹ = 512 ≤ 1000 < 1024 = 2¹⁰, so floor(log₂ 1000) + 1 = 10 bits.
Not quite. "10" read in base 16 is 1 × 16 + 0 = 16.
Not quite. Place values are b⁰, b¹, b², ..., each b times the last. Digits run 0 to b - 1, and the largest k-digit number is b^k - 1.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
What is the positional notation formula?
N = sum of d_i * b^i: each digit times the base to the power of its position, positions counted from 0 on the right. Every digit is less than the base.
Say your answer, then reveal it.
Why do computers use binary?
Electronic switches (transistors) have two reliable states, off and on, which map to 0 and 1. All data is stored as bits.
Say your answer, then reveal it.
42 in binary and in hex?
101010 (32 + 8 + 2) and 0x2A (2 * 16 + 10).
Say your answer, then reveal it.
How do you convert decimal to another base with division?
Repeat: record n % b, then n = n // b, until n is 0. Read the remainders bottom-up, because the first one is the lowest digit.
Say your answer, then reveal it.
What is Horner's method?
Base b to decimal, left to right: total = total * b + digit. 101010 gives 1, 2, 5, 10, 21, 42.
Say your answer, then reveal it.
What are the hex digits A to F worth?
A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. Trick: letter position plus 9.
Say your answer, then reveal it.
Why is one hex digit exactly four bits?
16 = 2^4, so a hex digit and a nibble both hold 16 values (0 to 15) and map one to one. Two hex digits make a byte.
Say your answer, then reveal it.
How do you turn binary into hex quickly?
Group the bits in fours from the right, pad the left group with zeros, and swap each group for one hex digit: 1101 0110 = D6.
Say your answer, then reveal it.
How many bits does n need?
floor(log2 n) + 1 for n >= 1, which is n.bit_length() in Python. 42 needs 6, a million needs 20.
Say your answer, then reveal it.
What are 0xFF, 0x100, 0x7F and 0x80?
255 (largest byte, 11111111), 256 (2^8), 127 (largest signed byte), 128 (only the top bit set).
Say your answer, then reveal it.
Which Python calls convert bases?
bin(n), hex(n), oct(n) to strings with prefixes; int(s, base) to parse; format(n, "08b") or f"{n:X}" for padding and case without prefixes.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.