Number systems and data representation
Computers store everything as bits: integers in binary and two's complement, text in Unicode, reals in IEEE 754 floating point.
Edited by Alex C, EduAlx · Sources cited below
Explanation
Binary, octal and hexadecimal are bases 2, 8 and 16. Two's complement represents negatives so addition works uniformly. IEEE 754 doubles have 53 bits of precision, so 0.1 + 0.2 ≠ 0.3 exactly. UTF-8 encodes all Unicode characters in 1–4 bytes and is backward-compatible with ASCII.
Example
Code
print(bin(45), hex(45)) # 0b101101 0x2d
print(0.1 + 0.2 == 0.3) # False
print('₹'.encode('utf-8')) # b'\xe2\x82\xb9'
How it works, step by step
- Choose the representation
- Convert by repeated division (integers) or multiplication (fractions)
- Store in fixed-width fields
- Be aware of overflow and rounding
Open the interactive page with diagrams →
People behind it
- Gottfried Wilhelm Leibniz (1646–1716), Mathematics and philosophy, Germany: Co-invented calculus with the notation we use today, developed binary arithmetic and built a calculating machine.
- William Kahan
- Ken Thompson (b. 1943), Computer science, USA: Co-created Unix and the B language, and later Go at Google. Turing Award 1983.
References
- IEEE Std 754-2019, Standard for Floating-Point Arithmetic.
- The Unicode Standard, Version 15, Unicode Consortium.
Textbooks
- Computer Organization and Design, David A. Patterson, John L. Hennessy (Morgan Kaufmann, RISC-V ed., 2nd ed. 2020). Standard computer architecture text.
- Digital Design, M. Morris Mano, Michael D. Ciletti (Pearson, 6th ed. 2018). Logic design fundamentals.
Related topics
RecursionBlockchain and distributed ledgersHeaps and priority queuesQuantum computingGreedy algorithms and minimum spanning treesSoftware engineering and version controlString algorithms and pattern matchingArtificial intelligence: search and the Turing testAutomata and formal languagesTransformers and large language models