Lesson 2.2: Representing Numbers, Text, Images and Sound
Introduction
In this lesson, we will explore how information is represented internally in computers using binary. By the end of this lesson, you should be able to:
- Understand how signed integers are represented and the idea of fixed- and floating-point numbers.
- Comprehend character encoding standards, specifically ASCII and Unicode, and their importance.
- Learn how images are represented as pixels alongside color depth and resolution.
- Understand how sound is represented through sampling.
- Describe how file size correlates with resolution, color depth, and sampling rate.
- Explain how integers and characters are represented in binary.
1. Representing Signed Integers
Signed integers are essential in computing as they allow the representation of both positive and negative whole numbers. In binary systems, this is achieved mainly through a method known as two's complement.
1.1 Two's Complement
Two's complement is the most common way to represent signed integers in binary. The process involves flipping the bits of a binary number and adding one. This approach simplifies binary arithmetic and accurately represents negative values.
Example 1: Converting Positive and Negative Integers
Consider the 8-bit representation:
- To represent the number $5$ in binary, we write it as:
$$5_{10} = 00000101_2$$
- To find the two's complement representation of $-5$, we first flip the bits:
$$\text{Flipped bits of } 00000101 = 11111010$$
- Next, we add $1$:
$$11111010 + 1 = 11111011$$
Thus, $-5$ is represented as $11111011$ in two's complement.
1.2 Fixed-point vs Floating-point Numbers
Numbers in computing can be represented in two primary formats: fixed-point and floating-point.
- Fixed-point representation has a set number of decimal places. It is straightforward but limited.
- Floating-point representation, defined by standards such as IEEE 754, allows representation of a wide range of values by using a mantissa and an exponent.
Example 2: Floating-point Representation
A floating-point number might look like this:
$$1.234 \times 10^3$$
In binary floating-point, the number is expressed as:
$$1.234_{10} = 1.001111011...\times 2^{11}$$
This system allows for handling very small and very large numbers efficiently.
2. Character Encoding
Character encoding is essential for representing text in computers. It determines how characters are mapped to binary data.
2.1 ASCII
The American Standard Code for Information Interchange (ASCII) uses $7$ bits to represent $128$ characters, including numbers, letters, and control codes.
Example 3: ASCII Representation
The letter 'A' is represented as:
$$A_{10} = 65_{10} = 01000001_2$$
2.2 Unicode
Unicode aims to provide a universal character set, encompassing thousands of characters from multiple languages.
It can be represented in several formats, including UTF-8, which uses one to four bytes per character.
Example 4: Unicode Representation
The letter 'A' in UTF-8 is:
$$A_{10} = 65_{10} = 01000001_2$$
For the character 'χ' (Greek chi), it would require two bytes:
$$χ_{10} = 0x03C7 = 11000010 10111001$$
3. Representing Images
Images are displayed on computers as a grid of pixels, each of which is assigned a color value. The resolution of an image defines how many pixels are present.
3.1 Pixels and Color Depth
Each pixel can display a certain number of colors, determined by the color depth. Common options are:
- 8 bits: $256$ colors
- 24 bits: over $16$ million colors (True Color)
Example 5: Color Representation
A pixel represented with $24$ bits might have the following RGB values:
$$\text{Red} = 255, \text{Green} = 0, \text{Blue} = 0$$
In binary, this is represented as:
$$\text{Red: } 11111111_2, \text{Green: } 00000000_2, \text{Blue: } 00000000_2$$
3.2 Resolution and File Size
File size is directly impacted by the resolution and color depth. For example, a $1920 \times 1080$ image with $24$ bits per pixel has a file size calculated as:
$$\text{File Size} = \text{Width} \times \text{Height} \times \text{Color Depth} = 1920 \times 1080 \times 3 \text{ bytes}$$
$$\text{File Size} = 6,220,800 \text{ bytes} \approx 6.2 \text{ MB}$$
4. Representing Sound
Sound in computers is represented through digital audio, where sound waves are sampled at discrete intervals.
4.1 Sampling
Sampling converts analog sound into digital format by recording the amplitude of sound waves at regular time intervals.
- The sampling rate determines how often these samples are taken, measured in Hertz (Hz).
- Common rates include $44.1 \text{kHz}$ for CDs.
Example 6: Sampling Rate and File Size
If a sound recording is $5 \text{ seconds}$ long sampled at $44.1 \text{kHz}$ with $16$ bits per sample:
$$\text{File Size} = \text{Sampling Rate} \times \text{Duration} \times \text{Bit Depth}$$
$$\text{File Size} = 44,100 \times 5 \times 2 = 441,000 \text{ bytes} \approx 441 \text{ KB}$$
Conclusion
In this lesson, we examined how numerical and textual data is represented in computers through different encoding schemes, including two's complement for integers and ASCII/Unicode for characters. We also discussed how images and sounds are represented via pixel color depth and sampling rates. Understanding these representations provides a foundation for further exploring data processing and manipulation in computers.
Study Notes
- Signed integers use two's complement for negative values.
- Fixed-point numbers have a set number of decimal places, while floating-point numbers can represent a wider range.
- ASCII is a $7$-bit standard for representing $128$ characters, while Unicode covers a broader set of characters.
- Images are represented as a grid of pixels, with file size determined by resolution and color depth.
- Sound files depend on the sampling rate and bit depth, influencing file size.
