Bits and binary numbers
Every computer, from a phone to a supercomputer, stores information using bits. Before talking about qubits, it helps to understand well what a bit is and how, by putting several together, we represent numbers.
The bit
A bit is the smallest unit of information: it has only two possible values, which we write as and . Physically it can be a low or high voltage on a wire, a discharged or charged capacitor, a hole or no hole in a card.
With bits we can form different sequences, because each new bit doubles the possibilities.
How many different sequences are there with 4 bits?
Mini-challenge
Numbers in binary
In the decimal system, each position is worth a power of 10. In binary, each position is worth a power of 2:
So the sequence represents the number
For example, .
What is the decimal value of ?
Mini-challenge
To go the other way, we divide the number by 2 repeatedly and read the remainders from bottom to top. For example, , so .
Write the number in binary.
Mini-challenge
Now in Python
In Python, int("101", 2) converts binary to decimal. But let us write the conversion ourselves, using the sum above. Complete the function so that it returns the decimal value of a string of bits.
Mini-challenge Β· Python
Input
bitsstrformat: text with only "0" and "1", at least 1 character
example:
"101"
Output
binary_to_decimalintformat: the decimal value
example:
5
Tests
Test 1 Example Not run yet
Call:
binary_to_decimal("101")Expected output:
5Test 2 Hidden Not run yet
Test 3 Hidden Not run yet
To go deeper: T. G. Wong, Introduction to Classical and Quantum Computing, ch. 1.
Solve this mini-lesson's mini-challenge to continue.