In this chapter:
- Learn how data is represented in the computer
- Build a working CPU emulator
- Create your own a numeric data type
- Understand the inner workings of floating point numbers
This chapter is all about understanding how zeroes and ones can be built up to become much larger objects such text, images and sound. We will also touch upon how computers do computation. By the end of the chapter, you will have emulated a fully-functional computer with CPU, memory and user-defined functions. You will break apart floating point numbers to create a numeric data type of your own making that only takes up a single byte. The chapter introduces a number of terms that may not be familiar to programmers who have never done systems programming, such as endianness and integer overflow.
A small but important lesson is that a single bit pattern can mean different things. The type system of a higher-level language, such as Rust, is just an artificial abstraction over reality. Understanding this becomes important as you begin to unravel some of that abstraction and to gain a deeper understanding of how computers work.
Listing 5.2 is an example that uses the same bit pattern to represent two different numbers. The type system—not the CPU—is what makes the distinction.
Listing 5.1. Output of Listing 5.2
a: 1100001111000011 50115 b: 1100001111000011 -15421