Why binary
A digital circuit reliably tells apart two states - a wire carrying a higher voltage or a lower one, a switch on or off. It cannot as reliably tell apart ten different voltage levels. So computers represent everything with two symbols, 0 and 1, the binary system.
Every number system has a base (radix) and place values that are powers of the base. Decimal (base 10) uses digits 0 to 9 with place values 1, 10, 100... Binary (base 2) uses 0 and 1 with place values 1, 2, 4, 8, 16... The value of a numeral is the sum of each digit times its place value.
Decimal, binary, octal, hexadecimal
- Decimal (base 10): digits 0-9.
- Binary (base 2): digits 0-1. Long to write, so it is grouped.
- Octal (base 8): digits 0-7. Each octal digit maps to exactly 3 binary digits.
- Hexadecimal (base 16): digits 0-9 then A-F for ten to fifteen. Each hex digit maps to exactly 4 binary digits.
Octal and hex exist because they are compact shorthands for binary that a person can read. One byte (8 bits) is two hex digits, for example.
Converting between bases
- Any base to decimal: multiply each digit by its place value and add. For binary 1011: 1 times 8, plus 0 times 4, plus 1 times 2, plus 1 times 1.
- Decimal to any base: repeatedly divide by the base and read the remainders from last to first.
- Binary to octal: group the bits into threes from the right and write each group's value.
- Binary to hexadecimal: group the bits into fours from the right and write each group's hex digit.
- Octal or hex to binary: replace each digit with its 3-bit or 4-bit pattern.
Worked example: conversions
- Binary 1011 to decimal. Place values from the right are 1, 2, 4, 8. So the value is 1 times 8, plus 0 times 4, plus 1 times 2, plus 1 times 1, which is 11.
- Decimal 45 to binary. 45 divided by 2 is 22 remainder 1; 22 is 11 r 0; 11 is 5 r 1; 5 is 2 r 1; 2 is 1 r 0; 1 is 0 r 1. Reading remainders bottom to top: 101101.
- Binary 101101 to hexadecimal. Group into fours from the right: 0010 1101. That is 2 and D, so 2D.
- Hexadecimal 2D to decimal. 2 times 16, plus 13 times 1, which is 45. This matches example 2, as it should.
Bits, bytes and the storage ladder
- A bit is one binary digit, 0 or 1 - the smallest unit.
- A nibble is 4 bits; a byte is 8 bits and is the usual unit for one character.
- Larger units step up by roughly a thousand: kilobyte (KB), megabyte (MB), gigabyte (GB), terabyte (TB), petabyte (PB).
Strictly, memory sizes step by 1024 (2 to the power 10), so 1 KB is 1024 bytes in that sense, while storage marketed in decimal uses 1000. The binary quantities are sometimes written KiB, MiB, GiB to avoid the ambiguity. For exams, know the order of the ladder and that a byte holds one character.
Representing characters: ASCII and Unicode
Text is stored as numbers using a character-encoding scheme.
- ASCII assigns a 7-bit code (0-127) to English letters, digits, punctuation and control characters. Extended ASCII uses 8 bits (0-255) for extra symbols. ASCII cannot represent most non-English scripts.
- Unicode assigns a unique code point to characters from nearly every writing system, including Devanagari and emoji. It is stored using an encoding such as UTF-8 (1 to 4 bytes per character; the first 128 code points match ASCII exactly), UTF-16 or UTF-32.
Because UTF-8 is backward-compatible with ASCII and space-efficient for Latin text, it is the dominant encoding on the web.
Common confusions
- Hex A-F are digits, not letters - they stand for ten to fifteen.
- When converting decimal to another base by division, read the remainders bottom to top, not top to bottom.
- A byte is 8 bits; a bit is a single binary digit. Data rates are often quoted in bits per second, storage in bytes - watch the small "b" versus capital "B".
- 1 KB is 1024 bytes in the memory sense but 1000 bytes in the marketing sense - both are used.
- ASCII is identical to Unicode's first 128 code points, and UTF-8 encodes them identically - Unicode did not "replace" ASCII so much as extend it.
- Octal groups bits in threes, hex in fours - do not mix them up.
Quick revision
- Computers use binary because a circuit reliably distinguishes two states.
- Bases: decimal 10, binary 2, octal 8, hex 16. One octal digit = 3 bits; one hex digit = 4 bits.
- To decimal: sum of digit times place value. From decimal: repeated division, remainders read bottom to top. Binary and octal/hex convert by grouping bits in threes or fours.
- Units: bit, nibble (4), byte (8, one character), then KB, MB, GB, TB, PB - by 1024 for memory, 1000 in marketing.
- ASCII: 7-bit English text (0-127). Unicode: a code point for every script, stored as UTF-8 (ASCII-compatible), UTF-16 or UTF-32.
Practice
Answer in one line each.
- What is the base of the binary number system?
- Convert binary 1010 to decimal.
- Convert decimal 5 to binary.
- How many different values can 4 bits represent?
- What is the decimal value of hexadecimal A?
- Which code represents English letters and symbols in 7 or 8 bits?
- Convert binary 1111 to decimal.
- Which standard extends character coverage to most of the world's scripts?