Number systems: practice problems with step-by-step solutions
Free Number systems practice problems with diagrams, hints and full worked solutions. New values every time, at three levels: binary and hex to decimal, two's complement, bits, ranges and memory.
Free practice problemEasy
Set the bits so the 8-bit register holds 0x99. What is the pattern's value N in decimal?
0x99
Click a bit to switch it between 0 and 1. The box shows your pattern as an unsigned number, and you can also type it. Exact answer: a whole number, no rounding.
In everyday life you count in decimal, base 10. It has ten digits, 0 to 9, and every place is worth ten times the place to its right.
In 352, the 3 is worth 300, the 5 is worth 50 and the 2 is worth 2. The places are worth 1, 10, 100 from the right.
352 = 3 · 100 + 5 · 10 + 2 · 1
A computer stores everything as switches that are on or off. So it counts in binary, base 2, with only the digits 0 and 1.
One binary digit is a bit. A group of 8 bits is a byte.
The rule is the same in every base: each digit is worth its value times a power of the base.
Self-check In the decimal number 4721, what is the 7 worth?
7000
700
70
7
Answer: 700. Counting from the right, the places are worth 1, 10, 100 and 1000. The 7 is in the third place, so it is worth 7 · 100 = 700.
2Binary and hexadecimal
In binary the places are worth 1, 2, 4, 8, 16 and so on, from the right. Each place is worth twice the place to its right.
For an 8-bit number the places are worth 128, 64, 32, 16, 8, 4, 2 and 1.
To read a binary number, add the place values of every bit that is 1. For 0011 0010 that is 32 + 16 + 2 = 50.
Long rows of bits are hard to read. Programmers often write them in hexadecimal (hex), base 16, instead.
Hex needs 16 digits. It uses 0 to 9 and then the letters A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15.
Hex places are worth 1, 16, 256 and 4096, from the right. A hex number is often written with 0x in front, for example 0x2F.
One hex digit is exactly 4 bits, so 0011 0010 is 0x32: 0011 is 3 and 0010 is 2.
Self-check What is 0x1A in decimal?
110
11
161
26
Answer: 26. A = 10. The places are 16 and 1, so 1 · 16 + 10 · 1 = 26. Writing the digits next to each other (110) or adding them (11) ignores the place values. 161 uses the places in the wrong order.
3Negative numbers: two’s complement
A byte with all places positive stores 0 to 255. This is called unsigned.
To store negative numbers, computers use two’s complement. The only change is that the leftmost bit is worth −128 instead of +128.
So 1000 0011 is −128 + 2 + 1 = −125. The leftmost bit is called the sign bit: when it is 1, the number is negative.
An 8-bit two’s complement number goes from −128 (1000 0000) to +127 (0111 1111).
To write a negative number −x, start from +x in binary, invert every bit (0 becomes 1, 1 becomes 0) and add 1.
Example for −5: 5 is 0000 0101. Inverted it is 1111 1010. Adding 1 gives 1111 1011.
The same pattern read as unsigned has the value 256 − x. For −5 that is 251, and 1111 1011 is indeed 128 + 64 + 32 + 16 + 8 + 2 + 1 = 251.
Self-check The 8-bit pattern 1111 1111 stores a two’s complement number. What is its value?
255
−127
−128
−1
Answer: −1. −128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = −1. 255 is the unsigned reading. −127 comes from treating the first bit as a minus sign in front of 111 1111.
4The method step by step
Binary or hex to decimal:
Write the place value under every digit: powers of 2 for binary, powers of 16 for hex, starting with 1 on the right.
In hex, replace each letter by its value: A = 10 up to F = 15.
Multiply each digit by its place value and add everything.
Two’s complement:
To read a pattern, give the leftmost bit the value −128 (in 8 bits) and add the place values of all 1-bits.
To write −x, write +x in 8 bits, invert every bit and add 1.
The unsigned value of that pattern is 256 − x. Use it to check your bits.
Bits, ranges and memory:
n bits give 2ⁿ different patterns.
Unsigned they store 0 to 2ⁿ − 1. In two’s complement they store −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.
To give k items their own code, find the smallest b with 2b ≥ k.
For memory, multiply the number of items by the bits per item, then divide by 8 for bytes and by 1024 for KiB.
M = width × height × bits per pixel / 8 / 1024 (KiB)
5A worked example
Follow every step. Try to predict the next one before you open it.
Worked example
The 8-bit pattern 1011 0110 stores a signed integer in two’s complement. What is its value N in decimal?
1
Write the place values
In two’s complement the leftmost place is worth −128. The other places are 64, 32, 16, 8, 4, 2 and 1.
2
Look at the sign bit
The leftmost bit is 1, so the number is negative.
3
Add the 1-bits
The 1-bits are in the places −128, 32, 16, 4 and 2.
N = −128 + 32 + 16 + 4 + 2 = −74
4
Check with the unsigned value
Read as unsigned, the pattern is 128 + 32 + 16 + 4 + 2 = 182.
For a negative number the unsigned value is 256 − x, so x = 256 − 182 = 74. That gives N = −74 again.
5
Check by inverting
Invert 1011 0110 to get 0100 1001, then add 1 to get 0100 1010.
0100 1010 is 64 + 8 + 2 = 74. So the original pattern is −74.
Answer
N = −74
6Common mistakes
Forgetting to add 1 after inverting the bits. You end up one too low.
Reading a two’s complement pattern as unsigned, for example 1111 1111 as 255 instead of −1.
Treating the sign bit as a minus sign in front of the other bits. In two’s complement it is worth −128.
Giving the places in the wrong order. The place worth 1 is always on the right.
Using A = 11 in hex. The letters start at A = 10.
Mixing bits and bytes in memory questions. Divide the number of bits by 8.
Self-check What is −5 as an 8-bit two’s complement pattern?
0000 0101
1000 0101
1111 1010
1111 1011
Answer: 1111 1011. 5 is 0000 0101. Inverting gives 1111 1010, and adding 1 gives 1111 1011. 1111 1010 forgets to add 1, and 1000 0101 puts a minus sign in front of 5.
7How exam questions look
Easy: binary and hex to decimal. Convert an 8-bit binary number or a 3-digit hex number to decimal.
Medium: two’s complement. Read the value of a signed 8-bit pattern, or write a negative number as a pattern and give its unsigned value.
Hard: bits, ranges and memory. The largest or most negative value that fits in n bits, the number of bits needed for k codes, or the memory taken by an image.
Example of a hard question: a 256 × 256 image with 24 bits per pixel takes 256 · 256 · 24 / 8 / 1024 = 192 KiB.
Self-check What is the smallest number of bits that gives 375 items their own code?
188
512
8
9
Answer: 9. 2⁸ = 256 is too few codes, 2⁹ = 512 is enough, so 9 bits. 512 is the number of codes, not the number of bits.
Convert the hexadecimal number 0x2BE to decimal. Find N.
0x2BEA = 10 … F = 15
Find N
Show hints
Hex places are worth 1, 16, 256, 4096 from the right.
Replace letters by their values (A = 10 … F = 15), multiply by the place value and add.
2 = 2, B = 11, E = 14.
Show the answer and the step-by-step solution
Positional number systems: each digit is worth its value times a power of the base. In binary the places are worth 1, 2, 4, 8, 16, …; in hexadecimal 1, 16, 256, …, with A–F standing for 10–15. In 8-bit two's complement the leftmost bit is worth −128 instead of +128, which is how negative numbers are stored.
Multiply each digit by its place value
2·256 + 11·16 + 14·1 = 702
Answer
0x2BE = 702
Example 2Medium
Set the bits so they store −45 in 8-bit two's complement. Read as an unsigned number, what is the pattern's value U?
N = −458 bit
Find U
Show hints
Write 45 in binary, invert every bit, then add 1.
45 = 0010 1101₂.
As an unsigned number the pattern equals 256 + N.
Show the answer and the step-by-step solution
Setting bits: each bit is worth a power of two (128, 64, …, 1). One hex digit is exactly four bits, and in 8-bit two's complement the leftmost bit is worth −128. Bitwise operations work on each bit position separately.
Invert and add one
45 = 0010 1101
inverted: 1101 0010
+1 → 1101 0011
Read it as unsigned
1101 0011₂ = 256 − 45 = 211
Answer
U = 211
Example 3Hard
Set the bits to the 8-bit result of 0111 0001 >> 3. What is the result's value R in decimal?
0111 0001>> 3
Find R
Show hints
Shifting right by 3 moves every bit 3 places to the right; bits that fall off the right end are lost and zeros come in on the left.
Work one bit position at a time, from left to right.
The first four bits of the result are 0000.
Show the answer and the step-by-step solution
Setting bits: each bit is worth a power of two (128, 64, …, 1). One hex digit is exactly four bits, and in 8-bit two's complement the leftmost bit is worth −128. Bitwise operations work on each bit position separately.
Bit by bit
Shifting right by 3 moves every bit 3 places to the right; bits that fall off the right end are lost and zeros come in on the left.
0111 0001 >> 3
= 0000 1110
Value of the result
0000 1110₂ = 14
Answer
R = 14
Questions and answers
What is Number systems?
In everyday life you count in decimal, base 10. It has ten digits, 0 to 9, and every place is worth ten times the place to its right.
In 352, the 3 is worth 300, the 5 is worth 50 and the 2 is worth 2. The places are worth 1, 10, 100 from the right.
How do I solve Number systems problems step by step?
Positional number systems: each digit is worth its value times a power of the base. In binary the places are worth 1, 2, 4, 8, 16, …; in hexadecimal 1, 16, 256, …, with A–F standing for 10–15. In 8-bit two's complement the leftmost bit is worth −128 instead of +128, which is how negative numbers are stored.
What do the Number systems exercises cover?
Easy: binary and hex to decimal
Medium: two's complement
Hard: bits, ranges and memory
Every problem has new values, so you learn the method instead of an answer. Stuck? Open a small hint first, and the full explanation only when you need it.
How practising works
Every problem has new values, so you learn the method instead of an answer.
Stuck? Open a small hint first, and the full explanation only when you need it.
Your progress, mistakes and saved problems are kept, so you can review before the exam.