Skip to main content
Logo image

Section 1.1 Bits, codes, and numbers

Simple binary choices.

The most basic idea in digital electronics is to represent the world using two values, 0 and 1. Here are some examples:
  • A claim, such as “It is raining outside” or “Jupiter is bigger than Mars”, is either TRUE (1) or FALSE (0).
  • The state of a light (or motor, or alarm) is either ON (1) or OFF (0).
  • On early computer displays, each pixel was either BLACK (0) or WHITE (1).
  • A clock display includes a light that is off (0) for AM, and on (1) for PM.
A two-alternative choice (1 or 0) is said to be binary. A single 1 or 0 is called a bit (a contraction of binary and digit). Representing the state of a light by 0 for OFF and 1 for ON uses one bit.

Checkpoint 1.1.1.

What other examples of binary states in the world can you think of?

Binary vectors.

When we need to represent more than two alternatives we can use more than one bit:
  • A thermostat could express the temperature of a room as TOO HOT (10), TOO COLD (01), or JUST RIGHT (00). (The remaining combination "11" could perhaps be assigned to signal a malfunction.)
  • A pair of bits can be used to control the headlights on a car: OFF (00), LOW BEAM (01), HIGH BEAM (11). The high beam is always on together with the low beam; the combination "10" would be unused.
  • Four shades of gray on a computer screen—WHITE, LIGHT GRAY, DARK GRAY, BLACK—can be represented by "11", "10", "01", and "00", respectively.
  • With three bits we can represent colors as mixtures of the primary colors red, green, and blue. Define RED by "100", GREEN by "010", and BLUE by "001". The combination of RED and BLUE is "101". Eight colors are possible in all, (Figure 1.1.2).
Color Binary code (RGB)
BLACK \(000\)
BLUE \(001\)
GREEN \(010\)
CYAN \(011\)
RED \(100\)
MAGENTA \(101\)
YELLOW \(110\)
WHITE \(111\)
Figure 1.1.2.
Groups of bits are called binary vectors, or bit vectors.
In the table above, the vector "100" is the binary code for the color RED. In general, if you have a vector consisting of \(N\) bits, you can represent \(2^N\) different binary codes and encode up to \(2^N\) different possibilties.

Checkpoint 1.1.3.

What other examples of multi-bit states in the world can you think of?

Checkpoint 1.1.4.

If you have an image display where each pixel is encoded by an 8-bit vector, how many shades of gray, from white to black, can you represent?
Answer.
With 8 bits, there can be \(2^8 = 256\) shades of gray.

Checkpoint 1.1.5.

If you have an image display where each pixel is encoded by 24 bits (eight for red, eight for green, and eight for blue), how many different colors can you make?
Answer.
With 24 bits, there can be \(2^{24} = 2^4 \cdot 2^{10} \cdot 2^{10}\) colors. Now, \(2^{10} = 1024 \approx 1000\text{,}\) so the number of colors is approximately 16 million.

Checkpoint 1.1.6.

How many bits should be required to encode all the alphanumeric characters 0..9, a..z, and A..Z? (We aren’t counting punctuation marks or special characters like "%".)
Answer.
There are 62 alphanumeric characters. The next power of two after 62 is \(64 = 2^6\text{.}\) Thus, six bits should be necessary. To include punctuation requires 7 bits. If you want to know how it’s really done, punctuation and all, visit the website www.asciitable.com
 1 
www.asciitable.com
.

Binary numbers.

Binary codes can be used to represent numbers. We’ll just do nonnegative numbers here, and introduce codes for negative numbers later. With three bits, we can represent the decimal numbers from 0 to 7 (Figure 1.1.7):
Decimal Binary
\(0\) \(000\)
\(1\) \(001\)
\(2\) \(010\)
\(3\) \(011\)
\(4\) \(100\)
\(5\) \(101\)
\(6\) \(110\)
\(7\) \(111\)
Figure 1.1.7.
The digits in a decimal number, from right to left, correspond to powers of ten (1, 10, 100…), and the system of decimal numbers is called “base 10”. Binary numbers obey place value just like decimal numbers, but based on powers of two rather than ten: from right to left, the bits correspond to powers of two (1, 2, 4…), and the system of binary numbers is called “base 2”.
In a decimal number, the leftmost digit, the one with the highest place value, is called the most significant digit. The rightmost digit, with the lowest place value, is called the least significant digit. Likewise, in a binary number, the leftmost bit is the most significant bit (msb), and the rightmost bit is the least significant bit (lsb). The bits of a number can be enumerated according to their significance, e.g., \(\mathbf{y} = (y_3, y_2, y_1, y_0)\text{:}\) \(y_3\) is the msb, and \(y_0\) is the lsb. Unlike the usual vector or n-tuple you may have seen in algebra or geometry, the indices of the bits in a binary number descend from 3 down to 0 rather than ascend from 0 to 3.
To facilitate interpretation of binary numbers, it’s a good idea to memorize a few powers of two (Figure 1.1.8).
Power Decimal Power Decimal
\(2^0\) \(1\) \(2^{10}\) \(1024, \text{"1K"}\)
\(2^1\) \(2\) \(2^{11}\) \(2048, \text{"2K"}\)
\(2^2\) \(4\) \(2^{12}\) \(4096, \text{"4K"}\)
\(2^3\) \(8\) \(2^{13}\) \(8192, \text{"8K"}\)
\(2^4\) \(16\) \(\vdots\)
\(2^5\) \(32\) \(2^{20}\) \(1024^2, \text{"1M"}\)
\(2^6\) \(64\) \(\vdots\)
\(2^7\) \(128\) \(2^{30}\) \(1024^3, \text{"1G"}\)
\(2^8\) \(256\) \(\vdots\)
\(2^9\) \(512\) \(2^{40}\) \(1024^4, \text{"1T"}\)
Figure 1.1.8.
In this table, \(2^{10} = 1024\) is approximately a thousand, and abbreviated "1K" (kilo). Likewise, \(2^{20}\text{,}\) \(2^{30}\text{,}\) and \(2^{40}\) are approximately a million, a billion, and a trillion, respectively, and are often abbreviated "1M" (mega), "1G" (giga), and "1T" (tera). Knowing these (or having the appropriate functions on your calculator), you can convert numbers from decimal to binary and back.

Checkpoint 1.1.9.

What decimal number is represented by the binary number 101101?
Answer.
\(1 \cdot 2^5 + 0 \cdot 2^4 + 1 \cdot 2^3 + 1 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 = 32 + 8 + 4 + 1 = 45\)

Checkpoint 1.1.10.

What is the binary representation of the decimal number 75?
Answer.
\begin{align*} 75 \amp= 64 + 8 + 2 + 1 \\ \amp= 1 \cdot 2^6 + 0 \cdot 2^5 + 0 \cdot 2^4 + 1 \cdot 2^3 + 0 \cdot 2^2 + 1 \cdot 2^1 + 1 \cdot 2^0 \\ \implies 75 \text{ decimal} \amp= 1001011 \text{ binary} \end{align*}

Binary, decimal, hexadecimal.

It can become tedious to write out all the 1s and 0s for even a moderately-sized binary number, not to mention being prone to error. A more compact way to write long binary numbers is to collect the bits into groups of four, and assign each four-bit code a unique character. The digits 0-9 are fine for "0000" through "1001". After that we use the letters A-F to denote ten ("1010") through fifteen ("1111"), respectively. A character in this system has 16 possibilities, and each place in a number represents a power of 16: \(16^0 = 1\text{,}\) \(16^1 = 16\text{,}\) \(16^2 = 256...\) . This base-16 number system is called hexadecimal. For example, decimal 108 is
\begin{equation*} \text{108 = 64 + 32 + 8 + 4 = 1101100 = 0110 1100 = 6C} \end{equation*}
in hexadecimal. Going the other way, A5 in hexadecimal is \(10 \cdot 16 + 5 \cdot 1 = 165\) in decimal.
When there is potential for confusion among binary, decimal, and hexadecimal, we can use subscripts, e.g., \(36_{16} = 110110_2 = 54_{10}\text{.}\) Another way is to precede a hexadecimal number by x or 0x, a decimal number by d or 0d, and a binary number by b or 0b: \(0\text{x}36 = 0\text{b}00110110 = 0\text{d}54\text{.}\)