Representations of Integers
Our ancestors learned to count early on. Fingers run out at , and that is why they invented positional notation.
Let with . Every positive integer can be written uniquely in the form
where , and for , and .
The digits are the remainders from Algorithm 2.1.
Base- expansion
- Input
- and with
- Output
- with
| 1 | ||
| 2 | ||
| 3 | while do | |
| 4 | // | |
| 5 | ||
| 6 |
Proof
Existence. Just before each test of the guard on line 3, the invariant is the conjunction of three clauses.
- Expansion. . The sum is empty, hence , when .
- Digits. for every .
- Bound. .
The variant is . Initialization and maintenance keep true at every test of the guard. Termination shows that the guard fails. Exit reads the expansion off at that test.
Initialization. After lines 1 and 2, and before the guard is tested, the expansion says . The digit clause is vacuous. The bound holds because .
Maintenance. Assume and the guard on line 3, and execute lines 4 and 5 once. The new digit satisfies , and the earlier digits are unchanged, so the digit clause holds at . The new quotient meets the bound. If , then
contradicting . The expansion carries to the next test of the guard:
Thus holds.
Termination. The bound clause says the variant is a nonnegative integer. Whenever the guard holds, the body replaces it by a strictly smaller one. Maintenance has already given , and
If , the right-hand side is at least . If , it equals . Thus . If the guard never failed, the successive values of the variant would be a strictly decreasing sequence of nonnegative integers, whose set of values would have no least element, contradicting Proposition 1.1. So the guard fails after finitely many iterations.
Exit. The guard on line 3 has failed, so , and still holds. The expansion collapses to
and the digit clause gives . Here : the case would mean . Line 6 sets . The last pass through line 4 began under the guard and produced , so and .
Uniqueness. Suppose and are two such expansions. Set for and for , and let . Then
Assume, on the contrary, that for some , and let be the least such index. The powers below cancel, so
The sum is empty when , and in every case the right-hand side is a multiple of . Thus . But , so . This is a contradiction. Therefore for every . In particular and , so .
In particular, setting , we get the following corollary, the binary expression of a positive integer. It is very common in computer science.
is called the base, or radix, of the expansion. We use the base- expansion as our canonical way of representing a positive integer, and we call it decimal notation. Base- expansions are called binary expansions, base- expansions are called octal expansions, and base- expansions are called hexadecimal expansions, also very common in computer science.
Binary digits are called bits in computer terminology. A byte is bits. An ASCII letter is one byte. The word code consists of letters.
| ASCII | decimal | binary | hex |
| c | |||
| o | |||
| d | |||
| e |
If you write exactly the content code into a file sample.txt via
printf 'code' > sample.txtthen sample.txt is exactly bytes, and you can view its hexadecimal representation with
xxd sample.txt# 00000000: 636f 6465 code