Chapter 2.9

Binary Streams, Octet-Vectors, and Bit-Vectors

"I counted to ten slowly, using binary notation."

Robert A. Heinlein, The Door Into Summer

Everything that happens inside a computer, happens as binary-encoded instruction sequences---and everything that is stored in memory or on your filesystem, is also stored in binary. When people think of "machine language", they often think in terms of assembly language---but even that is a transliteration for the benefit of humans. The machine---your computer---only understands 1's and 0's.

You can do a lot with high-level programming constructs, but when performance is critical, or you need to get down to the nitty-gritty for fine-tuned control, binary is your friend. And naturally, Lisp makes it as simple as possible to work with binary data, whether working over a network, accessing the file system, or connecting to hardware with a driver.

In this chapter we will go into much more detail on the subject of binary streams, octet-vectors, and bit-vectors. We've seen them all before, but only in passing---now it's time to take on the subjects of signed and unsigned bytes, little-endian and big-endian bit ordering, manipulating bits, conversion between bit-vectors, octet-vectors, and their integer equivalents, and of course, bit-wise logic and binary arithmetic. We have also already covered the binary, octet, and hexadecimal notation system for integers, but a formal tour of those notations will also be helpful to this discussion, so we'll tackle that first.

Exercise 2.9.1

Base and Order



Exercise 2.9.2

Byte-Vectors



Exercise 2.9.3

Bit-Vectors



Exercise 2.9.4

Integers as Bytes



Exercise 2.9.5

Unsigned Bytes



Exercise 2.9.6

Signed Bytes



Exercise 2.9.7

Big-Endian Byte Ordering



Exercise 2.9.8

Little-Endian Byte Ordering



Exercise 2.9.9

Bit-Shifting



Exercise 2.9.10

Bit-wise Logic



Exercise 2.9.11

Byte-Vector Arithmetic



Exercise 2.9.12

Bit-Vector Arithmetic



Exercise 2.9.13

Binary Streams



Exercise 2.9.14

Binary Sockets and Networks



Extra Credit Exercise 2.9.15

Simulating Hardware in Lisp



Extra Credit Exercise 2.9.16

Binary Instruction Sets



Extra Credit Exercise 2.9.17

Assembly Instructions



results matching ""

    No results matching ""