Lesson 01 / 16
Binary Number System
Positional number systems, binary representation, and conversion between bases.
Contents
Everything that resides in a computer’s memory — this lesson text, a photograph, the running program itself — is an object of the same kind: a sequence of two-state switches. This lesson asks the following question: how are numbers written in a medium that can hold nothing but two states?
The answer turns out to be a special case of a more general rule that already governs the number system used in daily life. Before turning to the binary system, it is worth looking carefully at what the decimal system actually does.
Positional Value
When 4703 is written in the decimal system, four separate symbols are placed side by
side, but the value expressed is not the sum of those symbols. Each symbol’s
contribution depends on its position:
This scheme is called positional notation. It has three components: a base , digits running from to , and exponents that increase from right to left. In general, a number with digits has the value:
The decimal system has nothing special about it; the base being ten is a historical consequence of having ten fingers on two hands. The rule works identically for every base. When the base is reduced to two, the digit set narrows to — which corresponds directly to two-state hardware.
Binary Representation
In the binary system the base is and the digits are and . A single binary digit is called a bit, a word that abbreviates “binary digit.” Positional values are powers of two:
The subscript states which base the number is written in. This marker is necessary:
the sequence 1011 reads as one thousand eleven if taken as decimal, and as eleven if
taken as binary. The fact that the same symbol sequence carries different values in
different bases will appear in a sharper form in later lessons — the same bit sequence
can be a number, a letter, or an instruction, depending on who interprets it.
The first ten binary numbers, with their counterparts:
| Decimal | Binary | Decimal | Binary |
|---|---|---|---|
| 0 | 0 | 5 | 101 |
| 1 | 1 | 6 | 110 |
| 2 | 10 | 7 | 111 |
| 3 | 11 | 8 | 1000 |
| 4 | 100 | 9 | 1001 |
A pattern stands out: every time the number reaches a power of two, one more digit
becomes necessary. The same thing that happens when decimal moves from 9 to 10
happens in binary when moving from 1 to 10.
Binary to Decimal
Converting a binary number to decimal is a direct application of the definition: sum the positional values of the bits. It suffices to add the positional values of the bits equal to , since bits equal to contribute nothing.
Here the positional values from right to left are ; the bits equal to sit at positions , , , and .
Decimal to Binary
The opposite direction uses repeated division. Divide the number by two, record the remainder, continue with the quotient; once the quotient reaches zero, read the remainders in reverse order.
For :
| Division | Quotient | Remainder |
|---|---|---|
| 26 | 1 | |
| 13 | 0 | |
| 6 | 1 | |
| 3 | 0 | |
| 1 | 1 | |
| 0 | 1 |
Reading the remainders from bottom to top yields , consistent with the value computed in the previous section.
Why the method works becomes clear from the definition of positional value. The remainder of dividing a number by two is that number’s rightmost bit: even numbers end in , odd numbers end in . Taking the quotient shifts the number one digit to the right, and the same question is then asked for the next bit.
Bits, Bytes, and Value Count
A single bit distinguishes two states. Two bits distinguish four, three bits distinguish eight. In general, bits can write
distinct patterns. This is the upper bound on representational capacity, and it is a counting fact independent of hardware: 8 bits allow 256 distinct patterns, and there is no 257th pattern.
A group of eight bits is called a byte. A byte is the smallest addressable unit of
memory; the processor reads at least a whole byte, never a single bit. When
interpreted as an unsigned integer, a byte holds values between and : the
smallest pattern is 00000000, the largest is 11111111, that is,
.
More generally, an -bit unsigned integer ranges between and . The arises because zero itself spends one pattern. Common widths and their bounds:
| Width | Pattern count | Largest unsigned value |
|---|---|---|
| 8 bit | 256 | 255 |
| 16 bit | 65,536 | 65,535 |
| 32 bit | 4,294,967,296 | 4,294,967,295 |
| 64 bit |
This table holds a bound that later lessons will return to often. A variable’s width determines the largest value it can hold; once that bound is exceeded, the number does not grow — its representation breaks. Overflow behavior will be detailed in the lesson on signed integers.
Seeing the Conversion in Code
The following program carries out the two conversions performed by hand in this lesson and compares the results against the built-in functions.
def decimal_to_binary(number: int) -> str: """Returns the binary representation of a non-negative integer.""" if number == 0: return "0" bits = [] while number > 0: bits.append(str(number % 2)) # remainder: rightmost bit number //= 2 # quotient: shift one digit right return "".join(reversed(bits)) def binary_to_decimal(representation: str) -> int: """Decodes a binary representation by summing positional values.""" value = 0 for bit in representation: value = value * 2 + int(bit) return value print(decimal_to_binary(53)) # 110101 print(binary_to_decimal("110101")) # 53 print(bin(53), int("110101", 2)) # 0b110101 53
The line value = value * 2 + int(bit) inside binary_to_decimal is the left-to-right
reading of the positional-value definition: each new bit shifts the value accumulated
so far one digit to the left and adds its own contribution.
Python’s built-in bin function returns the binary representation with a 0b prefix;
the second parameter of the int function specifies which base to read. This prefix is
only a notational convention, not part of the number itself.
Summary
- In positional notation, a digit’s contribution is determined by the base raised to its position; the decimal system carries no special privilege.
- In the binary system the base is and the digit set is ; a single binary digit is called a bit.
- Converting binary to decimal means summing the positional values of the bits equal to ; the reverse direction means reading the remainders of repeated division in reverse order.
- With bits, exactly distinct patterns can be written; under an unsigned interpretation the largest value is .
- A group of eight bits is called a byte, and memory is addressed at the byte level.
Next Step
Binary representation stays faithful to the hardware, but it is inconvenient for the human eye: a single 32-bit value is a sequence of zeros and ones that is hard to read without error. The next lesson takes up the hexadecimal base, which writes the same bit pattern with fewer symbols without breaking bit boundaries; that lesson will also establish a concrete example that the course will return to repeatedly.
To keep your progress and take notes, Log in
My notes
Log in to take notes.