4.5.2.1, 4.5.3.1–4.5.3.2 - Number bases and units of information

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 bb, integer place values from right to left are b0,b1,b2,…b^0,b^1,b^2,\ldots.

Decimal, also called denary, has base 10 and digits 0–9. Thus 34710=3×100+4×10+7×1347_{10}=3\times100+4\times10+7\times1. 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 102=21010_2=2_{10}, whereas 1016=161010_{16}=16_{10}. A subscript is a label for interpretation, not a multiplication. For example, 2F16=2×16+15=47102F_{16}=2\times16+15=47_{10}. A written binary numeral cannot contain digit 2; a hexadecimal numeral cannot use G.

Leading zeros preserve the value: 001012=1012=51000101_2=101_2=5_{10}. 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 1011012101101_2, the six positions have weights 32, 16, 8, 4, 2, 1, so 1011012=32+8+4+1=4510101101_2=32+8+4+1=45_{10}.

For decimal to binary, work from the largest useful power of two. To represent 931093_{10} 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 01011101201011101_2. 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 16=2416=2^4. Each hexadecimal digit corresponds to exactly four bits, often called a nibble. The following table lets you translate each group independently.

HexBinaryHexBinary
0000081000
1000191001
20010A1010
30011B1011
40100C1100
50101D1101
60110E1110
70111F1111

Group binary bits in fours from the right, padding the leftmost group with zeros if necessary. Thus 10111012=0101  11012=5D161011101_2=0101\;1101_2=5D_{16}. In the reverse direction, A616=1010  01102A6_{16}=1010\;0110_2. 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. 254÷16=15254\div16=15 remainder 14, so 25410=FE16254_{10}=FE_{16}. For larger values continue dividing the quotient, then read the remainders backwards. Hexadecimal to decimal uses powers of 16, for example 1A316=1×256+10×16+3=419101A3_{16}=1\times256+10\times16+3=419_{10}. 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 nn bits, the number of different bit patterns is 2n2^n.

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 28=2562^8=256 possible patterns.

If a field needs at least 40 distinct states, five bits are insufficient because 25=322^5=32, while six bits provide 26=642^6=64. 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 prefixByte symbolBytesBinary prefixByte symbolBytes
kilo, kkB10310^3kibi, KiKiB2102^{10}
mega, MMB10610^6mebi, MiMiB2202^{20}
giga, GGB10910^9gibi, GiGiB2302^{30}
tera, TTB101210^{12}tebi, TiTiB2402^{40}

For example, 1 kB=1000 B1\,\mathrm{kB}=1000\,\mathrm{B}, but 1 KiB=1024 B1\,\mathrm{KiB}=1024\,\mathrm{B}. 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 3 MiB3\,\mathrm{MiB} to bytes, multiply by 2202^{20}: 3×1 048 576=3 145 728 B3\times1\,048\,576=3\,145\,728\,\mathrm{B}. To express this in MB, divide by 10610^6, giving 3.145728 MB3.145728\,\mathrm{MB}. Conversely, 5 000 000 B÷220≈4.76837 MiB5\,000\,000\,\mathrm{B}\div2^{20}\approx4.76837\,\mathrm{MiB}. 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.