4.5.2.1, 4.5.3.1–4.5.3.2 - Number bases and units of information
In 4.5.2.1 you will read and convert decimal, binary and hexadecimal values. In 4.5.3.1–4.5.3.2 you will connect bits to possible values and convert byte quantities using precise decimal and binary prefixes.
Denary, binary and hexadecimal
A digit's contribution depends on both the digit and its position in a number.
Number base
A number base gives the number of different digits used and the multiplier between consecutive place values. In base , integer place values from right to left are .
Decimal, also called denary, has base 10 and digits 0–9. Thus . Binary has base 2 and digits 0 and 1; its places are 1, 2, 4, 8, 16 and so on. Hexadecimal has base 16 and sixteen digits: 0–9 followed by A, B, C, D, E and F, whose values are 10, 11, 12, 13, 14 and 15.
The subscript identifies the base, so , whereas . A subscript is a label for interpretation, not a multiplication. For example, . A written binary numeral cannot contain digit 2; a hexadecimal numeral cannot use G.
Leading zeros preserve the value: . They may still matter when a question requests a fixed width. Give eight bits when it asks for an eight-bit representation.
Converting bases
Converting a number changes its notation while retaining its value.
For binary to decimal, add the place values whose bits are 1. For , the six positions have weights 32, 16, 8, 4, 2, 1, so .
For decimal to binary, work from the largest useful power of two. To represent in eight bits, use weights 128, 64, 32, 16, 8, 4, 2, 1. Choose 64, leaving 29; skip 32; choose 16, leaving 13; choose 8, leaving 5; choose 4, leaving 1; skip 2; choose 1. The answer is . Repeated division by two is an alternative: record each remainder and read the remainders from the last division to the first.
Hexadecimal is especially convenient because . Each hexadecimal digit corresponds to exactly four bits, often called a nibble. The following table lets you translate each group independently.
| Hex | Binary | Hex | Binary |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | A | 1010 |
| 3 | 0011 | B | 1011 |
| 4 | 0100 | C | 1100 |
| 5 | 0101 | D | 1101 |
| 6 | 0110 | E | 1110 |
| 7 | 0111 | F | 1111 |
Group binary bits in fours from the right, padding the leftmost group with zeros if necessary. Thus . In the reverse direction, . Hexadecimal is a shorter, easier-to-read notation for a bit pattern; it does not compress the stored data or change its value.
For decimal to hexadecimal, divide by 16 and use the quotient/remainder. remainder 14, so . For larger values continue dividing the quotient, then read the remainders backwards. Hexadecimal to decimal uses powers of 16, for example . Conversion through binary is equally valid.
Bits, bytes and possible values
Storage capacity depends on the number of independent bit positions, rather than the visual length of a decimal value.
A bit is the fundamental unit of information and has a value of either 0 or 1. A byte is a group of eight bits. A bit pattern receives meaning from an encoding: the same pattern could represent an integer, a character or something else.
With bits, the number of different bit patterns is .
Each additional bit doubles the choices. Three bits give eight patterns: 000, 001, 010, 011, 100, 101, 110, 111. These are eight patterns even though, interpreted as unsigned integers, the largest value is seven. Do not confuse number of values with maximum value. An eight-bit byte has possible patterns.
If a field needs at least 40 distinct states, five bits are insufficient because , while six bits provide . Choose the smallest integer number of bits whose pattern count covers the demand; unused patterns are possible.
Decimal and binary byte prefixes
A capacity conversion must preserve whether the prefix refers to a power of ten or a power of two.
| Decimal prefix | Byte symbol | Bytes | Binary prefix | Byte symbol | Bytes |
|---|---|---|---|---|---|
| kilo, k | kB | kibi, Ki | KiB | ||
| mega, M | MB | mebi, Mi | MiB | ||
| giga, G | GB | gibi, Gi | GiB | ||
| tera, T | TB | tebi, Ti | TiB |
For example, , but . The capital B denotes bytes. Bit quantities use a lowercase b; convert bytes to bits by multiplying by eight. Adjacent decimal prefixes differ by a factor of 1000, while adjacent binary prefixes differ by 1024.
To convert to bytes, multiply by : . To express this in MB, divide by , giving . Conversely, . Write the unit so that the result's interpretation is clear.
Historically, “kilobyte” and “megabyte” have sometimes been used when kibibyte and mebibyte were intended. Do not silently assume the historical meaning: use the explicit symbol or the definition supplied by the task. A device described in GB and software reporting in GiB may show different numbers for the same byte quantity.
First convert to bytes using the stated prefix; then convert to the requested unit. Powers of ten, powers of two and the factor of eight each answer a different conversion.