A field of n bits in a computer can hold exactly 2ⁿ different values, the numbers from 0 to 2ⁿ − 1. This lesson works out the ranges of the common widths, how each further bit doubles them, how many bits a given range of values needs, how much memory an n-bit address can reach, and the off-by-one errors that come from counting values from 0.
The values of an n-bit field
Two results are already proved. By the theorem “what k bits can name” of Number Bases lesson 1.2, the bit strings of length k name exactly the 2ᵏ numbers 0, 1, …, 2ᵏ − 1. By Theorem 1 of lesson 1.1, k yes-or-no questions single out one of at most 2ᵏ alternatives. This lesson looks at the same fact from the side of the machine, where the bits come in fields of fixed width.
A field is a part of a record, a message or a memory unit that is set aside for one piece of information, with a fixed number of bits, its width. The header of every Internet packet, for example, begins with a Version field of 4 bits, which tells the format of the header.[1]
Let n ≥ 1.
- A field of n bits has exactly 2ⁿ different contents. Read as binary numerals, they are the numbers 0 to 2ⁿ − 1.
- Whatever its contents are made to stand for, a field of n bits can tell apart at most 2ⁿ different things.
Both parts are results already proved, applied to a field. (1) The contents of the field are the bit strings of length n, and by the theorem “what k bits can name”, with k = n, there are exactly 2ⁿ of them, naming 0 to 2ⁿ − 1.
(2) To be told apart, different things need different contents, and by part (1) there are only 2ⁿ contents. This is the argument of the Proposition “binary digits and bits” of lesson 1.2, and the same bound as part (2) of Theorem 1 of lesson 1.1, where k yes-or-no answers single out at most 2ᵏ alternatives.
Part (2) matters because the bits of a field do not have to stand for the numbers 0 to 2ⁿ − 1 (lesson 1.3). They can stand for negative numbers, letters or colors. The values change; their number does not: never more than 2ⁿ.
| Width n | Values 2ⁿ | Largest 2ⁿ − 1 |
|---|---|---|
| 1 | 2 | 1 |
| 4 | 16 | 15 |
| 8 | 256 | 255 |
| 16 | 65, | 65, |
| 32 | 4, | 4, |
| 64 | 18, | 18, |
Another field of the Internet header, IHL, also has 4 bits. It gives the length of the header in units of 32 bits, and a correct header has at least 5 of them.[2] How many values can the field hold, and how long can a header be?
- By Proposition 1, 4 bits hold 2⁴ = 16 values, the numbers 0 to 15.
- The values 0 to 4 are never used in a correct header, so the lengths that occur are 5 to 15 units: 15 − 5 + 1 = 11 of them.
- The longest header has 15 units of 32 bits: 15 × 32 = 480 bits, and 480 ÷ 8 = 60 bytes.
Answer: 16 values, of which 11 occur, and a header of at most 60 bytes. A rule of the format can leave some patterns unused; nothing can add patterns beyond the 16.
In the Internet’s Transmission Control Protocol, TCP, every message carries a source port number and a destination port number, each in a field of 16 bits.[3] How many port numbers are there, and what is the largest?
- 2¹⁶ = 2⁸ × 2⁸ = 256 × 256 = 65,536 (lesson 1.3).
- The values are 0 to 2¹⁶ − 1 = 65,535.
Answer: 65,536 port numbers, from 0 to 65,535. The largest is not 65,536, which would need 17 bits.
Each bit doubles
The table grows fast: 8 bits hold 256 values, 16 bits more than sixty-five thousand, 64 bits a number of twenty digits. The reason is that a further bit does not add a fixed amount of values: it doubles them.
Let n ≥ 1 and m ≥ 1.
- A field of n + m bits holds 2ᵐ times as many values as a field of n bits: 2ⁿ⁺ᵐ = 2ⁿ × 2ᵐ. In particular one more bit doubles them.
- The largest value of n + 1 bits is twice the largest value of n bits, plus 1: 2ⁿ⁺¹ − 1 = 2 × (2ⁿ − 1) + 1.
(1) 2ⁿ⁺ᵐ is a product of n + m factors 2. By the associative property they can be grouped into the first n, whose product is 2ⁿ, and the other m, whose product is 2ᵐ.
(2) 2 × (2ⁿ − 1) + 1 = 2ⁿ⁺¹ − 2 + 1 = 2ⁿ⁺¹ − 1. In bits: the largest value of n bits is n 1s; writing a 1 after them doubles it and adds 1 (Number Bases lesson 1.2), and gives the n + 1 1s of the largest value of n + 1 bits.
Ten bits give 2¹⁰ = 1,024 values (Number Bases lesson 1.2), a little more than a thousand. So every ten further bits multiply the number of values by 1,024, a little more than 1,000:
- 2²⁰ = 1,024 × 1,024 = 1,048,576, a little more than a million;
- 2³⁰ = 1,048,576 × 1,024 = 1,073,741,824, a little more than a billion;
- 2⁴⁰ = 1,073,741,824 × 1,024 = 1,099,511,627,776, a little more than a trillion.
The excess grows: 2⁴⁰ is about 10% more than a trillion. The next lesson shows why this nearly-a-thousand caused a lasting confusion in the units of storage.
Someone reasons that 32 bits are twice 16 bits, so a 32-bit field holds twice as many values as a 16-bit field. How many times as many does it hold?
- By Theorem 2 with n = m = 16: 2³² = 2¹⁶ × 2¹⁶.
- So 32 bits hold 2¹⁶ = 65,536 times as many values as 16 bits, not 2 times.
- Check: 2³² = 65,536 × 65,536 = 4,294,967,296, and 4,294,967,296 ÷ 65,536 = 65,536.
Answer: 65,536 times as many. Adding bits multiplies the number of values; doubling the bits squares it.
Without multiplying it out, show that 2⁶⁴, the number of values of a 64-bit field, has twenty digits.
- By Theorem 2, 2⁶⁴ = 2⁴ × 2⁶⁰ = 16 × 2⁶⁰, and the sixty factors of 2⁶⁰ group into six groups of ten: 2⁶⁰ is a product of six factors 1,024.
- Each 1,024 is more than 1,000, so 2⁶⁰ is more than 1,000 × 1,000 × 1,000 × 1,000 × 1,000 × 1,000, which is 1 followed by 18 zeros (powers of ten). Each 1,024 is less than 1,000 × 1.1, and 1.1 × 1.1 × 1.1 × 1.1 × 1.1 × 1.1 = 1.771561 is less than 2 (multiplying decimals), so 2⁶⁰ is less than 2 followed by 18 zeros.
- So 2⁶⁴ = 16 × 2⁶⁰ lies between 16 followed by 18 zeros and 32 followed by 18 zeros: both bounds have 20 digits.
Answer: 20 digits. Indeed 2⁶⁴ = 18,
How many bits a range of values needs
The question can also be turned round: given the values that a field must hold, how wide must it be? Lesson 1.1 answered it for N alternatives: the fewest bits is the k with 2ᵏ⁻¹ < N ≤ 2ᵏ, because k bits give 2ᵏ patterns and k − 1 bits only 2ᵏ⁻¹. In a computer the values are usually described not by their number but by their range, and the first step is to count them correctly.
Let a ≤ b be whole numbers, and let N = b − a + 1.
- There are exactly N whole numbers from a to b, both included.
- If N ≥ 2, they can be stored in k bits, by storing x − a in place of x, where k is the number with 2ᵏ⁻¹ < N ≤ 2ᵏ; and no code of k − 1 bits can tell them apart. In particular the numbers 0 to M need the k with 2ᵏ⁻¹ ≤ M < 2ᵏ, that is, as many bits as the binary numeral of M has.
(1) Subtracting a pairs the numbers a, a + 1, …, b one to one with the numbers 0, 1, …, b − a: each x goes to x − a, and each d comes back as a + d. Adding 1 to each of 0, …, b − a pairs these one to one with 1, …, b − a + 1, a list of b − a + 1 number words. So there are b − a + 1 = N of them (Counting lesson 1.1).
(2) The differences x − a run from 0 to N − 1. Since N ≤ 2ᵏ, N − 1 ≤ 2ᵏ − 1, so each difference has a k-bit pattern, and different numbers have different differences. With k − 1 bits there are only 2ᵏ⁻¹ < N patterns, too few by Proposition 1, as in the Proposition “questions for N alternatives” of lesson 1.1. For a = 0 and b = M, N = M + 1, and 2ᵏ⁻¹ < M + 1 ≤ 2ᵏ says 2ᵏ⁻¹ ≤ M < 2ᵏ: by Number Bases lesson 1.1, Exercise 7, M then has exactly k binary digits.
A field must hold every number from 0 to 256. Someone notes that 256 = 2⁸ and chooses 8 bits. Is that enough?
- The numbers from 0 to 256 are 256 − 0 + 1 = 257 values, one more than 2⁸.
- 2⁸ = 256 < 257 ≤ 512 = 2⁹, so 9 bits are needed. Indeed 256 = 100000000₂ has 9 binary digits.
- 256 values, say the numbers 0 to 255, would fit in 8 bits exactly.
Answer: no, 9 bits. The largest value 256 and the number of values 257 differ by one, and here that one changes the width.
A field stores a year from 1900 to 2155 as its difference from 1900. How many bits does it need, and how is 2026 stored?
- N = 2155 − 1900 + 1 = 256 = 2⁸, so 8 bits, and no fewer.
- 2026 − 1900 = 126 = 64 + 32 + 16 + 8 + 4 + 2, stored as 01111110.
- Storing the year itself, a number up to 2,155, would need 12 bits, since 2¹¹ = 2,048 ≤ 2,155 < 4,096 = 2¹².
Answer: 8 bits, and 2026 is stored as 01111110 = 126. Storing the difference saves 4 bits; the year 2156 would need a ninth bit.
The Unicode Standard numbers characters from 0 to 0x10FFFF, 1,114,112 numbers in all (lesson 1.4).[4] How many bits does one such number need?
- N = 1,114,112. 2²⁰ = 1,048,576 < 1,114,112 ≤ 2,097,152 = 2²¹, so 21 bits.
- In hexadecimal: 0x10FFFF is the digit 1, one bit, followed by five digits of four bits each: 1 + 5 × 4 = 21 bits.
Answer: 21 bits. Three bytes, 24 bits, hold any of them, with 3 bits to spare.
A counter starts at 0 and must count up to 1,000,000 events. How many bits does it need?
- It must hold the numbers 0 to 1,000,000: N = 1,000,001.
- 2¹⁹ = 524,288 < 1,000,001 ≤ 1,048,576 = 2²⁰, so 20 bits.
Answer: 20 bits. Here the extra value 0 does not change the width, because 1,000,001 is still at most 2²⁰.
Addresses
Memory is a row of bytes, each with an address of its own (lesson 1.2), and the addresses are numbered from 0: in the WebAssembly standard, for example, an address is the zero-based index of a byte in memory (lesson 1.3).[5] An address is itself a number kept in a field of fixed width, and that width decides how many bytes the addresses can name.
- Addresses of n bits name exactly 2ⁿ bytes, at the addresses 0 to 2ⁿ − 1.
- A memory of S ≥ 2 bytes needs addresses of k bits, where 2ᵏ⁻¹ < S ≤ 2ᵏ. Its last address is S − 1.
(1) The addresses are the contents of an n-bit field read as numbers, so by Proposition 1 they are the 2ⁿ numbers 0 to 2ⁿ − 1. (2) The S bytes have the addresses 0 to S − 1, which are S − 1 − 0 + 1 = S numbers; by Proposition 3 with a = 0 and b = S − 1 they need k bits.
How many bytes can 16-bit addresses reach, and what are the first and the last address, in decimal and in hexadecimal?
- By Proposition 4, 2¹⁶ = 65,536 bytes.
- The addresses run from 0 to 65,535, that is from 0x0000 to 0xFFFF: four hexadecimal digits for 16 bits (lesson 1.4).
Answer: 65,536 bytes, from 0x0000 to 0xFFFF. A 65,537th byte would need a seventeenth address bit.
WebAssembly measures memory in pages of 65,536 bytes.[6] How many pages do 32-bit addresses reach? How many address bits does a memory of 3 pages need, and what is its last address?
- A page is 65,536 = 2¹⁶ bytes. 32-bit addresses reach 2³² bytes, and by Theorem 2, 2³² = 2¹⁶ × 2¹⁶: 2¹⁶ = 65,536 pages.
- 3 pages are 3 × 65,536 = 196,608 bytes. 2¹⁷ = 131,072 < 196,608 ≤ 262,144 = 2¹⁸, so 18 address bits.
- The last address is 196,608 − 1 = 196,607 = 2 × 65,536 + 65,535, that is 0x2FFFF.
Answer: 65,536 pages; 18 bits, and the last address is 196,607 = 0x2FFFF. Of the 2¹⁸ = 262,144 addresses of 18 bits, the last 65,536 name no byte.
The addresses of the original Internet Protocol have 32 bits;[7] those of IPv6 have 128 bits.[8] Someone concludes that IPv6 has four times as many addresses. How many times as many can it write?
- By Proposition 1, 32 bits give 2³² patterns and 128 bits give 2¹²⁸.
- By Theorem 2, 2¹²⁸ = 2³² × 2⁹⁶. So IPv6 can write 2⁹⁶ times as many addresses, not 4 times.
- 2⁹⁶ is a product of 96 factors 2; grouped as 2⁶ × 2⁹⁰, it is 64 times nine factors 1,024, and so more than 64 followed by 27 zeros.
Answer: 2⁹⁶ = 79,
Off-by-one errors
Because the values of a field are counted from 0, the largest value is one less than the number of values, and the last address of a memory one less than its size. Getting such a count wrong by exactly one is so common that it has a name: an off-by-one error is the use of a maximum or minimum value that is one more, or one less, than the correct value.[9]
Three facts, each proved above, settle the counts of this lesson:
- Values and largest value. n bits hold 2ⁿ values; the largest is 2ⁿ − 1 (Proposition 1).
- Counting a range. The whole numbers from a to b are b − a + 1; the difference b − a counts the steps between them, not the numbers (Proposition 3).
- Where a block ends. S bytes starting at the address A occupy A to A + S − 1; the next free address is A + S. This is the second fact with a = A and b = A + S − 1.
A block of 100 bytes starts at the address 0x2000. What is its last address, and where does the next block start?
- The block occupies 0x2000 to 0x2000 + 99. In hexadecimal 99 = 6 × 16 + 3 = 0x63.
- 0x2000 + 0x63 = 0x2063, and the next free address is 0x2000 + 100 = 0x2064.
Answer: the last byte is at 0x2063, and the next block starts at 0x2064. Writing 0x2064 as the last address would claim a 101st byte.
How many bytes lie at the addresses from 0x1000 to 0x1FFF? Someone answers 0x1FFF − 0x1000 = 0xFFF.
- 0x1FFF − 0x1000 = 0xFFF = 4,095 counts the steps from the first byte to the last.
- The bytes are one more: 0xFFF + 1 = 0x1000 = 16³ = 4,096.
Answer: 4,096 bytes, exactly 2¹² of them. The block is the 12-bit addresses 0x000 to 0xFFF, moved up by 0x1000.
The 256 values of a byte are listed in increasing order. Which value is the 256th, and is there a 257th?
- The first value is 0, the second 1, and in general the k-th value is k − 1.
- So the 256th value is 255 = 11111111, the largest pattern.
- There is no 257th: it would be 256, which needs 9 bits.
Answer: 255; there is no 257th. Position and value differ by one whenever counting starts at 0.
Fit the field
- Round
- 1
- Score
- 0
- Streak
- 0
A counter must hold every whole number from 0 to 1,024.
How many different values must the field tell apart, and what is the fewest bits that do it?
4 bits have 2⁴ = 16 patterns: the numbers 0 to 15.
Exercises
How many values does a field of (a) 12 bits and (b) 24 bits hold, and what is the largest?
▸ Show solution
(a) 2¹² = 4,096 values, the largest 4,095 = 0xFFF.
(b) 2²⁴ = 2¹² × 2¹² = 4,096 × 4,096 = 16,777,216 values, the largest 16,777,215 = 0xFFFFFF.
What is the fewest bits for (a) the numbers 0 to 999; (b) 1,024 different items; (c) the numbers 1 to 1,024, stored as their difference from 1; (d) the numbers 0 to 1,024?
▸ Show solution
(a) 0 to 999 are 1,000 values: 2⁹ = 512 < 1,000 ≤ 1,024 = 2¹⁰, so 10 bits.
(b) 1,024 = 2¹⁰ values: exactly 10 bits.
(c) 1 to 1,024 are 1,024 − 1 + 1 = 1,024 values: 10 bits, storing x − 1.
(d) 0 to 1,024 are 1,025 values: 2¹⁰ = 1,024 < 1,025 ≤ 2,048 = 2¹¹, so 11 bits.
A sensor reports a temperature from −40 to 85 degrees, in whole degrees, stored as its difference from −40. How many bits does it need, and how is 25 degrees stored?
▸ Show solution
The values run from −40 to 85: 85 − (−40) + 1 = 85 + 40 + 1 = 126 values. 2⁶ = 64 < 126 ≤ 128 = 2⁷, so 7 bits, storing t − (−40) = t + 40.
25 degrees is stored as 25 + 40 = 65 = 64 + 1, the pattern 1000001.
How many bytes can 20-bit addresses reach? Give the last address in decimal and in hexadecimal.
▸ Show solution
2²⁰ = 1,048,576 bytes. The last address is 2²⁰ − 1 = 1,048,575; in hexadecimal, 20 bits are five digits, and the largest is 0xFFFFF.
How many bytes lie at the addresses from 0x3F00 to 0x40FF, both included?
▸ Show solution
0x40FF − 0x3F00 = 0x01FF = 511 steps, so 511 + 1 = 512 bytes. Check: 0x3F00 to 0x3FFF are 256 bytes and 0x4000 to 0x40FF another 256.
A byte is split into a 5-bit field and a 3-bit field. How many different combinations of the two contents are there? Compare with one byte.
▸ Show solution
A 5-bit field has 2⁵ = 32 contents and a 3-bit field 2³ = 8. Each content of the first can go with each content of the second: 32 groups of 8, that is 32 × 8 = 256 combinations.
By Theorem 2 this is 2⁵ × 2³ = 2⁸, the number of contents of one byte. Cutting the 8 bits of a byte into fields, as in the layouts of lesson 1.2, changes what the bits mean, never how many different bytes there are.
A field of n bits stores the numbers from a to a + 2ⁿ − 1 by their difference from a. Prove that every number of this range has exactly one pattern and that a + 2ⁿ cannot be added to the range.
▸ Show solution
The differences x − a of the numbers a to a + 2ⁿ − 1 are 0 to 2ⁿ − 1, which are exactly the n-bit patterns read as numbers (Proposition 1); different numbers have different differences, so each number has exactly one pattern, and every pattern is used.
Since every pattern is already used, a + 2ⁿ has none left: the numbers a to a + 2ⁿ are 2ⁿ + 1 values, more than the 2ⁿ patterns, so by Proposition 1, part (2), no code of n bits can tell them all apart. Stored as a difference, it would need 2ⁿ, whose binary numeral is 1 followed by n zeros: n + 1 bits.
Summary
- A field of n bits has exactly 2ⁿ contents: as binary numerals, the numbers 0 to 2ⁿ − 1. Whatever they stand for, it can tell apart at most 2ⁿ things.
- 8, 16, 32 and 64 bits hold 256, 65,536, 4,294,967,296 and 18,446,744,073,709,551,616 values; the largest value is always one less.
- One more bit doubles the number of values, and m more bits multiply it by 2ᵐ. Every ten bits multiply it by 1,024, a little more than a thousand.
- The numbers from a to b are b − a + 1 values. They fit in the k bits with 2ᵏ⁻¹ < b − a + 1 ≤ 2ᵏ, storing x − a; the numbers 0 to M need as many bits as M has binary digits.
- Addresses of n bits reach 2ⁿ bytes, at 0 to 2ⁿ − 1. A block of S bytes at A ends at A + S − 1. Mistakes of exactly one in such counts are called off-by-one errors.
Sources
- 1.Information Sciences Institute, University of Southern California (J. Postel, ed.), “RFC 791: Internet Protocol”, RFC Editor, Internet Engineering Task Force (September 1981).Section “3.1, “Internet Header Format”: Version”. rfc-editor.org/rfc/rfc791.html · open at the quoted passage.
“Version: 4 bits The Version field indicates the format of the internet header.”
Used for: the header of every Internet packet begins with a Version field of 4 bits, which tells the format of the header (the word “field” in this sense). - 2.Information Sciences Institute, University of Southern California (J. Postel, ed.), “RFC 791: Internet Protocol”, RFC Editor, Internet Engineering Task Force (September 1981).Section “3.1, “Internet Header Format”: IHL”. rfc-editor.org/rfc/rfc791.html · open at the quoted passage.
“IHL: 4 bits Internet Header Length is the length of the internet header in 32 bit words, and thus points to the beginning of the data. Note that the minimum value for a correct header is 5.”
Used for: the IHL field has 4 bits and gives the length of the header in units of 32 bits; a correct header has at least 5 of them. - 3.W. Eddy (ed.), “RFC 9293: Transmission Control Protocol (TCP)”, RFC Editor, Internet Engineering Task Force (August 2022).Section “3.1, “Header Format””. rfc-editor.org/rfc/rfc9293.html · open at the quoted passage.
“Source Port: 16 bits The source port number. Destination Port: 16 bits The destination port number.”
Used for: every TCP header carries a source port number and a destination port number, each in a field of 16 bits. - 4.The Unicode Consortium, The Unicode Standard, Version 18.0: Core Specification (Unicode Consortium, South San Francisco, 2026).Chapter 2, “General Structure”, section 2.4, “Code Points and Characters”, p. 65 (the whole topic: pp. 65–66).
“In the Unicode Standard, the codespace consists of the integers from 0 to 10FFFF₁₆, comprising 1,114,112 code points available for assigning the repertoire of abstract characters.”
Used for: the Unicode numbers run from 0 to 0x10FFFF, 1,114,112 numbers in all. - 5.Andreas Rossberg (ed.), “WebAssembly Core Specification (W3C Recommendation)”, World Wide Web Consortium (W3C) (5 December 2019).Section “2.4.4, “Memory Instructions””. w3.org/TR/wasm-core-1 · open at the quoted passage.
“The static address offset is added to the dynamic address operand, yielding a 33 bit effective address that is the zero-based index at which the memory is accessed.”
Used for: in the WebAssembly standard an address is the zero-based index of a byte in memory: the addresses are numbered from 0. - 6.Andreas Rossberg (ed.), “WebAssembly Core Specification (W3C Recommendation)”, World Wide Web Consortium (W3C) (5 December 2019).Section “4.2.8, “Memory Instances””. w3.org/TR/wasm-core-1 · open at the quoted passage.
“The length of the vector always is a multiple of the WebAssembly page size, which is defined to be the constant 65536 – abbreviated 64Ki.”
Used for: WebAssembly measures memory in pages of 65,536 bytes. - 7.Information Sciences Institute, University of Southern California (J. Postel, ed.), “RFC 791: Internet Protocol”, RFC Editor, Internet Engineering Task Force (September 1981).Section “2.3, “Function Description”: Addressing”. rfc-editor.org/rfc/rfc791.html · open at the quoted passage.
“Addresses are fixed length of four octets (32 bits).”
Used for: the addresses of the original Internet Protocol (IPv4) have 32 bits. - 8.R. Hinden and S. Deering, “RFC 4291: IP Version 6 Addressing Architecture”, RFC Editor, Internet Engineering Task Force (February 2006).Section “2, “IPv6 Addressing””. rfc-editor.org/rfc/rfc4291.html · open at the quoted passage.
“IPv6 addresses are 128-bit identifiers for interfaces and sets of interfaces”
Used for: IPv6 addresses have 128 bits. - 9.“CWE-193: Off-by-one Error”, Common Weakness Enumeration (CWE), sponsored by the U.S. Cybersecurity and Infrastructure Security Agency (CISA) and managed by The MITRE Corporation.Section “Description”. cwe.mitre.org/data/definitions/193.html · open at the quoted passage.
“A product calculates or uses an incorrect maximum or minimum value that is 1 more, or 1 less, than the correct value.”
Used for: the name off-by-one error, and its definition: a maximum or minimum value that is one more, or one less, than the correct value.