Chapter 23: Bits and Bytes

Part 1: What is a Bit?

bit (short for binary digit) is the most basic unit of information in computing. It can only have one of two possible values: 0 or 1.

Think of a bit like a simple light switch:

  • 0 represents OFF, or false, or no voltage.

  • 1 represents ON, or true, or voltage present.

That’s it! A single bit can only tell us one of two things. It’s not much on its own, but when you combine many bits together, they can represent incredibly complex information.

Why binary? Because computers are made of millions of tiny electronic switches (transistors). These switches can only be in one of two states: on or off. So the binary system (base-2) is a natural fit for how computers are built.

python

Part 2: What is a Byte?

byte is a group of 8 bits treated as a single unit. It’s the fundamental unit of storage in most computer systems. Just as 8 bits make a byte, bytes combine to make kilobytes, megabytes, gigabytes, and so on.

text

Why 8 bits? Historically, it was a convenient size that could represent a single character (like ‘A’ or ‘z’) and has become the standard across virtually all computer architectures.

With 8 bits, we can represent 2⁸ = 256 different values (from 0 to 255). This is enough to represent all lowercase letters, uppercase letters, digits, punctuation marks, and special characters—which brings us to character encoding.

Part 3: From Bits to Numbers

How do we represent numbers using only 0s and 1s? We use the binary number system (base-2).

In our everyday decimal system (base-10), each digit’s place value is a power of 10:

  • The number 3,472 means: (3 × 10³) + (4 × 10²) + (7 × 10¹) + (2 × 10⁰)

In binary (base-2), each digit’s place value is a power of 2:

  • The rightmost bit is the 1’s place (2⁰)

  • The next bit is the 2’s place (2¹)

  • Then the 4’s place (2²)

  • Then the 8’s place (2³)

  • And so on…

Let’s convert the binary number 1011 to decimal:

Bit Position 8’s place (2³) 4’s place (2²) 2’s place (2¹) 1’s place (2⁰)
Binary 1 0 1 1
Value 1 × 8 = 8 0 × 4 = 0 1 × 2 = 2 1 × 1 = 1

Total: 8 + 0 + 2 + 1 = 11

So the binary number 1011 equals the decimal number 11.

python

Output:

text

Part 4: From Bytes to Text (Character Encoding)

So we can represent numbers using bits and bytes. But how do we represent text? This is where character encoding comes in. The most fundamental encoding is ASCII (American Standard Code for Information Interchange).

ASCII assigns a unique number (from 0 to 127) to each character. Since 7 bits can represent 128 values (2⁷ = 128), ASCII fits comfortably in a single byte (with one bit left unused).

Let’s see how the word “Hi” is stored in a computer:

text

python

Output:

text

Unicode: ASCII is limited to English characters. To represent characters from all the world’s writing systems, we have Unicode, which uses up to 4 bytes per character. UTF-8, the most common encoding, uses a variable number of bytes (1-4) per character, with the first 128 characters matching ASCII for backward compatibility.

Part 5: Units of Storage

Just as we have units like “inch,” “foot,” and “mile” for distance, we have units for digital information. Here they are, from smallest to largest:

Unit Abbreviation Approximate Size Analogy
Bit b 1 binary digit A single light switch
Byte B 8 bits A single character (like ‘A’)
Kilobyte KB 1,024 bytes A short paragraph
Megabyte MB 1,024 KB (about 1 million bytes) A digital photo
Gigabyte GB 1,024 MB (about 1 billion bytes) A full-length movie
Terabyte TB 1,024 GB (about 1 trillion bytes) A large hard drive
Petabyte PB 1,024 TB All the data in a large data center

Note: There’s a slight confusion in terminology. In some contexts (especially hard drive marketing), “kilobyte” means exactly 1,000 bytes. But in programming and computer science, it’s almost always 1,024 (2¹⁰) bytes. This is because computers work in powers of 2, not powers of 10.

python

Part 6: Practical Examples – Seeing Bits and Bytes in Action

Example 1: How Many Values Can N Bits Represent?

The number of distinct values you can represent with N bits is 2ᴺ.

python

Example 2: Looking at the Bits of a Number

Let’s write a simple function to see the binary representation of a number with a fixed number of bits.

python

Output:

text

Example 3: How Text is Stored

Let’s see how a simple message is stored in bytes.

python

Output:

text

So the word “Hello” is stored in your computer’s memory as:
01001000 01100101 01101100 01101100 01101111

Example 4: Estimating File Sizes

Let’s estimate how much storage different types of content would require.

python

Part 7: Bitwise Operations (Manipulating Individual Bits)

Because bits are so fundamental, programming languages provide special operators to work directly with them. These are called bitwise operators. They operate on the individual bits of a number.

python

These operations are extremely fast and are used in graphics, cryptography, networking, and systems programming.

Summary: The Bits and Bytes Philosophy

  • bit is the most basic unit of information: a single 0 or 1.

  • byte is 8 bits, the fundamental unit of storage.

  • Bits and bytes allow computers to represent numbers using the binary system.

  • They represent text through character encodings like ASCII and Unicode.

  • Storage units (KB, MB, GB, TB) are powers of 2 (usually) or powers of 10 (sometimes).

  • With N bits, you can represent 2ᴺ different values.

  • Bitwise operators allow direct manipulation of individual bits for maximum efficiency.

Understanding bits and bytes is like understanding the alphabet of computing. It’s the foundation upon which everything else—from simple text files to complex operating systems—is built. Once you grasp this, you’ll have a much deeper appreciation for how computers store, process, and transmit information.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *