Back to series
· 4 min read

How Is Data Represented in Computers? Bits, Bytes, and Encoding


I. What Exactly Is Data in a Computer?

What we see in computers is very diverse:

  • Text: articles, code, chat logs.
  • Numerical values: integers, decimals, monetary amounts.
  • Images: photos, icons, screenshots.
  • Audio: music, speech, recordings.
  • Video: movies, animations, live streams.
  • Programs: applications, operating systems, scripts.

These types of data look completely different, but at the underlying level of a computer, they all ultimately need to be converted into binary data.

Computers use sequences of data composed of 0s and 1s to represent information, and then interpret that data through different encoding rules.

For example, the same binary sequence:

01000001

can have different interpretations:

Interpretation Result
Unsigned integer 65
ASCII character A
Hexadecimal 0x41
Binary value 01000001

This illustrates an important point:

Binary data itself does not inherently represent text, numbers, or images; what truly determines its meaning is the rules used to interpret it.

1. Three Levels of Data Representation

Understanding computer data representation can be divided into three levels:

  1. Physical layer: stores and transmits information through circuit states.
  2. Binary layer: uses 0s and 1s to form data sequences.
  3. Semantic layer: interprets data according to rules for integers, floating-point numbers, characters, images, etc.
flowchart TD
    A["Information in the real world"]
    B["Data encoding rules"]
    C["Binary data"]
    D["Physical storage and transmission"]
    E["Read binary data"]
    F["Decode according to encoding rules"]
    G["Restore to text, numbers, images, etc."]

    A --> B
    B --> C
    C --> D
    D --> E
    E --> F
    F --> G

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F3EAFE,stroke:#B9A1DB,color:#51416F
    style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style D fill:#FFF3D9,stroke:#E7C77D,color:#765A22
    style E fill:#F1F5F9,stroke:#CBD5E1,color:#334155
    style F fill:#F3EAFE,stroke:#B9A1DB,color:#51416F
    style G fill:#DCEFE5,stroke:#8EBFA5,color:#24563F

II. Bit: The Smallest Basic Unit of Computer Data

Bit is short for Binary Digit, also known as a binary digit.

A bit can only store two states:

0 \quad \text{or} \quad 1

1. How Many States Can One Bit Represent?

A bit has two possibilities:

2^1=2

If two bits are used:

00
01
10
11

There are:

2^2=4

combinations.

If three bits are used:

2^3=8

combinations.

Therefore, the number of distinct states that n bits can represent is:

N=2^n
Number of Bits Number of Combinations
1 Bit 2
2 Bit 4
3 Bit 8
4 Bit 16
8 Bit 256
16 Bit 65,536
32 Bit Approx. 4.295 billion
64 Bit Approx. 1.84467 × 10^19

This means that as the number of binary digits increases, the amount of information that can be expressed grows rapidly.

2. How Can Bits Represent Multiple States?

Suppose you need to represent a simple device state:

  • 0: Off.
  • 1: On.

One bit is enough.

If you need to represent four states:

  • 00: Standby.
  • 01: Running.
  • 10: Paused.
  • 11: Fault.

Then two bits are needed.

If you need to represent 100 different states, how many bits are required at minimum?

Because:

2^6=64
2^7=128

So at least 7 bits are needed.

Generally, the minimum number of bits required to represent N states is:

n=\lceil\log_2N\rceil

3. Bits Are More Than Numbers

Bits can represent more than mathematical values; they can also represent:

  • A switch state.
  • A permission flag.
  • A Boolean value.
  • A color component.
  • Part of the information in a character encoding.
  • A network protocol field.

For example, a permission field can use three bits:

001: Read permission
010: Write permission
100: Execute permission

Multiple bits can be combined to represent multiple permissions.

III. Byte: The Most Commonly Used Data Unit in Computers

A bit is the basic unit of information, but computers generally use the byte as an important unit for data organization and addressing.

1. How Many Bits Are in a Byte?

In modern mainstream computers:

1Byte=8Bits

For example:

01000001

is one byte.

Eight bits can represent:

2^8=256

different combinations.

The value range of an unsigned integer is:

0\sim255

2. Why Use 8 Bits as One Byte?

Eight bits is a widely adopted standard in modern computer systems.

It can provide 256 combinations, making it suitable for representing:

  • English letters.
  • Numbers.
  • Punctuation marks.
  • Common control characters.
  • Small-range integers.
  • Color channel data.

Note that, historically, there have been byte designs with different bit widths in computing. In the C language standard, a byte is the smallest addressable storage unit, and CHAR_BIT describes how many bits a byte contains.

Modern mainstream platforms typically:

#include <limits.h>
#include <stdio.h>

int main() {
    printf("CHAR_BIT = %d\n", CHAR_BIT);
    return 0;
}

Output:

CHAR_BIT = 8

3. Differences Between Bits and Bytes

Comparison Bit Byte
Chinese Bit Byte
Meaning Binary digit Byte
Size 1 binary digit Usually 8 bits
Main use Represent a single logical state Data storage and addressing
Common unit b B

Pay special attention to case:

  • b stands for bit.
  • B stands for byte.

For example:

8Mbps=1MB/s

This assumes decimal units are used and ignores protocol overhead.

Network bandwidth is usually measured in bit/s, while file sizes and storage capacities are usually measured in bytes.

4. Common Data Capacity Units

The following units are commonly used in computers:

Unit Decimal definition Binary definition
KB / KiB 1000 B 1024 B
MB / MiB 1000 KB 1024 KiB
GB / GiB 1000 MB 1024 MiB
TB / TiB 1000 GB 1024 GiB

Strictly speaking, KB, MB, and GB are decimal units, while KiB, MiB, and GiB are binary units.

For example:

1KB=1000Bytes
1KiB=1024Bytes
1MB=1000000Bytes
1MiB=1048576Bytes

5. Why does a 1TB hard drive show less than 1TB of capacity?

Hard drive manufacturers usually calculate capacity in decimal.

For example:

1TB=10^{12}Bytes

If the operating system converts it using TiB:

\frac{10^{12}}{1024^4}\approx0.9095TiB

So a hard drive advertised as 1TB may show about 0.91 TiB in the operating system.

In addition, filesystem formatting, partitioning, and system-reserved space can further reduce the available capacity.

IV. How Does Binary Represent Integers?

Computers store integers in binary, and different representation rules determine what a number means.

1. Place Values in Binary

Decimal uses 10 as its base:

123=1\times10^2+2\times10^1+3\times10^0

Binary uses 2 as its base:

1011_2=1\times2^3+0\times2^2+1\times2^1+1\times2^0

Therefore:

1011_2=8+0+2+1=11

Reading binary from right to left, the place values are, in order:

2^0,2^1,2^2,2^3,\ldots

2. Converting Binary to Decimal

For example:

110101_2

Expand it:

\begin{aligned} 110101_2=&1\times2^5\\ &+1\times2^4\\ &+0\times2^3\\ &+1\times2^2\\ &+0\times2^1\\ &+1\times2^0 \end{aligned}

Calculate:

32+16+4+1=53

Therefore:

110101_2=53_{10}

3. Converting Decimal to Binary

The common method is to repeatedly divide by 2, record the remainder each time, and then read the results from bottom to top.

For example, to convert 25 to binary:

Operation Quotient Remainder
25 ÷ 2 12 1
12 ÷ 2 6 0
6 ÷ 2 3 0
3 ÷ 2 1 1
1 ÷ 2 0 1

Read from bottom to top:

25_{10}=11001_2

4. Hexadecimal: A Compact Form of Binary

Binary is well suited to computer processing, but it has many digits and is inconvenient for humans to read.

For this reason, hexadecimal is often used in software development.

Hexadecimal uses:

0 1 2 3 4 5 6 7 8 9 A B C D E F

Each hexadecimal digit corresponds to 4 binary bits.

Binary Hexadecimal
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

For example:

11010110_2

Group every 4 bits:

1101 0110

Convert to hexadecimal:

D6

Therefore:

11010110_2= D6_{16}

Hexadecimal is commonly used for:

  • Memory addresses.
  • Binary file analysis.
  • Network packets.
  • Color encoding.
  • Machine instructions.
  • Debuggers and logs.

V. How Do Computers Represent Signed Integers?

Computers need to represent not only positive numbers but also negative numbers.

Common integer representation methods include:

  • Unsigned integers.
  • Sign-magnitude.
  • Ones' complement.
  • Two's complement.

Modern computers typically use two's complement to represent signed integers.

1. Unsigned Integers

Suppose 8 bits are used to represent an unsigned integer.

There are a total of:

2^8=256

combinations.

Value range:

0\sim255

For example:

00000101 = 5
11111111 = 255

2. Sign-Magnitude

Sign-magnitude uses the highest bit to represent the sign:

  • 0 indicates a positive number.
  • 1 indicates a negative number.

The remaining bits represent the absolute value.

For example, with 8-bit representation:

+5 = 00000101
-5 = 10000101

Sign-magnitude has two zeros:

+0 = 00000000
-0 = 10000000

This increases the complexity of computation and comparison.

3. Ones' Complement

The ones' complement of a positive number is the same as its sign-magnitude representation.

The ones' complement of a negative number is obtained by inverting all bits of the corresponding positive number.

For example:

+5 = 00000101
-5 = 11111010

Ones' complement also has positive zero and negative zero.

4. Two's Complement

Two's complement is the most commonly used method for representing signed integers in modern computers.

The process for calculating the two's complement of a negative number:

  1. Write out the binary representation of the corresponding positive number.
  2. Invert all bits.
  3. Add 1.

For example, representing -5 using 8 bits:

+5          = 00000101
Invert bits = 11111010
Add 1       = 11111011

Therefore:

-5=11111011

5. Why Use Two's Complement?

Two's complement allows subtraction to be converted into addition.

For example:

7-5=7+(-5)

Using 8-bit two's complement:

  00000111
+ 11111011
------------
1 00000010

Ignoring the carry beyond 8 bits:

00000010 = 2

Therefore:

7-5=2

Hardware can use an adder to perform addition and subtraction without needing to design completely separate operation circuits for each case.

6. Value Range of Two's Complement

The range of n-bit two's complement is:

-2^{n-1}\sim2^{n-1}-1

For example, 8-bit two's complement:

-128\sim127

Note that the ranges of positive and negative numbers in two's complement are not completely symmetric.

7. Integer Overflow

Suppose an 8-bit unsigned integer is used:

255+1=256

But 8 bits can only represent 0 to 255.

If only the lower 8 bits are kept:

  11111111
+ 00000001
------------
1 00000000

The result becomes:

00000000

For unsigned integers, this is equivalent to modulo 2^8 arithmetic.

256\bmod256=0

In C/C++, a special distinction must be made:

  • Unsigned integer overflow is handled according to modulo rules.
  • Signed integer overflow is undefined behavior.

One cannot assume that all programming languages use the same overflow rules.

VI. How Do Computers Represent Floating-Point Numbers?

Integers can be represented directly through binary place values, but decimals require more complex representation methods.

Modern computers typically adopt the IEEE 754 floating-point standard.

1. Binary Decimals

The place values to the right of the binary decimal point are, in order:

2^{-1},2^{-2},2^{-3},\ldots

For example:

0.101_2

Expanded:

0.101_2= 1\times2^{-1} +0\times2^{-2} +1\times2^{-3}
=0.5+0+0.125

Therefore:

0.101_2=0.625_{10}

2. IEEE 754 Single-Precision Floating-Point Numbers

A 32-bit single-precision floating-point number consists of three parts:

Field Bits Purpose
Sign bit 1 Indicates positive or negative
Exponent bits 8 Represents the exponent
Mantissa bits 23 Represents the significant digits

Structure:

S | EEEEEEEE | MMMMMMMMMMMMMMMMMMMMMMM

For a normal number:

Value=(-1)^S\times(1.M)\times2^{E-127}

Where:

  • S: the sign bit.
  • E: the encoded exponent.
  • M: the mantissa part.
  • 127: the exponent bias for single-precision floating point.

3. Why do floating-point numbers have precision errors?

The decimal 0.1 cannot be represented exactly with a finite number of binary fractional digits.

Therefore, a computer can only store the closest approximation.

For example:

a = 0.1
b = 0.2

print(a + b)

It may output:

0.30000000000000004

This is not a calculation error; it is caused by the limited precision of binary floating point.

For financial amounts, precise measurements, and similar scenarios, consider:

  • Representing the smallest unit as an integer.
  • Using the Decimal type.
  • Using fixed-point numbers.

4. Special values of floating point

IEEE 754 also defines special values:

Type Meaning
+0 Positive zero
-0 Negative zero
+Infinity Positive infinity
-Infinity Negative infinity
NaN Not a number
Subnormal Subnormal number

For example:

import math

x = float("nan")

print(math.isnan(x))

To test for NaN, you should use a dedicated detection method rather than a simple equality comparison.