Study Atlas › Computer Science › Computer fundamentals

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

–5 in 8-bit two's complement is 11111011; 'A' is 0x41 in ASCII/UTF-8.

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

  1. Choose the representation
  2. Convert by repeated division (integers) or multiplication (fractions)
  3. Store in fixed-width fields
  4. Be aware of overflow and rounding

Open the interactive page with diagrams →

People behind it

References

  1. IEEE Std 754-2019, Standard for Floating-Point Arithmetic.
  2. The Unicode Standard, Version 15, Unicode Consortium.

Textbooks

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