Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Learning Outcomes

2Introduction

In this section, we discuss how computer architects and computer scientists translate between the rich world that humans see and information that computers store. The former is framed by how humans think—after all, we have ten fingers, also known as “digits”. The latter is in bits.

3Numerals as a representation of Numbers

Let’s discuss the idea of formally representing a number with many possible numerals, i.e., symbols.

"A diagram features a horizontal gold abstraction line separating the word Numeral at the top from the word Number at the bottom. This visual layout reinforces the caption by positioning numerals as the symbolic representations above the line and numbers as the underlying abstract concepts below it."

Figure 1:Numerals (and therefore digits) are representations of numbers.

Figure 2 is a motivating (and humorous) example. An alien and an astronaut are discussing how to represent the number of rocks in a pile.

"A astronaut talking to an alien with 2 fingers per hand and there are 4 rocks on the ground. Alien says  'There are 10 rocks.' Astronaut says 'Oh, you must be using base 4. See, I use base 10.' Alien says 'No. I use base 10. What is base 4?' Caption reads 'Every base is base 10'"

Figure 2:Every base is base 10 (web.archive.org)

The alien, the astronaut, and the pile of rocks use three different representations of the number four. The astronaut uses the numeral 4 to represent four as a base-10 integer. The alien uses the numeral 10 to represent four as a base-4 integer. The pile of rocks uses four rocks to represent four as, well, a pile of rocks.

4Binary, Decimal, and Hexadecimal Representations

While there are an infinite number of bases with which to represent numbers, we discuss three will be the most useful to us, as computer scientists: decimal, binary, and hexadecimal representations.

Table 1 probably makes little sense to you at the moment, but we present it first so you can make some educated guesses.

Table 1:Decimal, binary, and hexadecimal digits.

System# DigitsDigits
Decimal100, 1, 2, 3, 4, 5, 6, 7, 8, 9
Binary20, 1
Hexadecimal160, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

4.1Decimal: Base 10 (Ten) Numbers

First, consider decimal numerals. The decimal numeral 3271 is written in that order, because it describes how to count powers of ten corresponding to the equation below (note the superscript 10 denotes a base-10 numeral):

3271=327110=(3×103)+(2×102)+(7×101)+(1×100)\begin{align} 3271 &= 3271_{10} \\ &= (3 \times 10^3) + (2 \times 10^2) + (7 \times 10^1) + (1 \times 10^0) \end{align}

Each of the four digits specify a count of a power of ten. We add these up to get the number three thousand, two hundred seventy-one.

This process will seem natural to you—because we humans think in base ten—but we will see that we can apply this understanding to represent numbers in other bases. Nevertheless, we highlight a few implicit assumptions:

4.2Base 2 (Two) Numbers, Binary

Binary numbers like 1101 are written in that order to describe how to count powers of two. Notably, the two binary digits, 0 and 1, are the namesake of the bit (which takes on those two values).

What does the binary numeral 1101 represent?

Because humans think in decimal, we convert this binary value to decimal with a similar process as above:

0b1101=11012=(1×23)+(1×22)+(0×21)+(1×20)=8+4+0+1=13\begin{align} \texttt{0b1101} &= 1101_{2} \\ &= (1 \times 2^3) + (1 \times 2^2) + (0 \times 2^1) + (1 \times 2^0) \\ &= 8 + 4 + 0 + 1 \\ &= 13 \end{align}

Other notes:

4.3Base 16 (Sixteen) #s, Hexadecimal

Finally, we consider hexadecimal numbers.

What does the hexadecimal numeral A5 represent?

Convert to decimal:

0xA5=A516=(10×161)+(5×160)=160+5=165\begin{align} \texttt{0xA5} &= A5_{16} \\ & = (10 \times 16^1) + (5 \times 16^0) \\ & = 160 + 5 \\ & = 165 \end{align}

We prepend the prefix 0x to denote that the numeral A5 should be interpreted in base 16. Like before, the shorthand 0xA5 is equivalent to A516A5_{16} but can be written with a standard keyboard.

Hexadecimal digits are useful as shorthand for representing groups of four binary digits. We discuss more at the end of this section.

5Convert between representations

Table 2:First sixteen numbers as decimal, hexadecimal, and binary representations.

NumberHexadecimalBinary
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
10A1010
11B1011
12C1100
13D1101
14E1110
15F1111

Let’s discuss conversion in more detail. We only consider “unsigned” numerals, i.e., non-negative numbers.

If we have an nn-digit unsigned numeral dn1d_{n-1} dn2d_{n-2}...d0d_0 in radix (or base) rr, then the value of that numeral is:

i=0n1ridi\sum_{i=0}^{n-1} r^i d_i

which is just fancy notation to say that instead of a 10’s or 100’s place we have an rr’s or r2r^2’s place. For the three radices binary, decimal, and hex, we just let rr be 2, 10, and 16, respectively.

5.1Decimal \rightarrow Binary

The slidedeck below shows how we can convert the decimal 13 into its binary representation, 0b1101.


Let val be 13 in the explanation below. Click to show.

The process above relies on a few colloquial observations:

Here is one attempt at colloquially describing the algorithm to convert a number val into its binary representation:

5.2Decimal \rightarrow Hexadecimal

The slidedeck below converts 165 into its hexadecimal representation, 0xA5.

Let val be 165 in the explanation below. Click to show.

We leave it to you to translate the binary conversion process we described colloquially into a hexadecimal conversion process.

5.3Binary \leftrightarrow Hexadecimal Is Straightforward

Given the above, consider the following process for converting to binary to hexidecimal, which composes the processes we’ve discussed above:

  1. Convert binary to decimal.

  2. Convert decimal to hexadecimal.

This process is tedious—computing powers of twos is doable, but does every computer architect memorize powers of sixteen? Instead, we can directly convert between binary and hexadecimal with the observation:

There exists a one-to-one mapping between the set of hexadecimal digits and the set of length-four binary strings.

The above observation implies that a 4k4k-length binary string can be translated into a kk-length hexadecimal string by independently converting each length-4 binary string into a hexadecimal digit, then concatenating the result. (We leave the proof of this to those of you that are enthusiastic mathematicians.) This makes conversion between binary and hexadecimal much easier:

To convert 0x1E to binary:

To convert 0b11110 to hexadecimal:

6The computer knows it, too

At this point, it’s worthwhile to remind first-time readers that the two-character prefix 0b and 0x denote that the digits should be interpreted as binary and hexadecimal representations, respectfully. The 0 in 0b and 0x doesn’t mean anything by itself.

These prefixes allow computers to parse strings of digits and interpret them in their intended base.

#include <stdio.h>
int main() {
  const int N = 1234;
  printf("Decimal: %d\n",N);
  printf("Hex: 	  %x\n",N);
  printf("Octal:   %o\n",N);

  printf("Literals (not supported by all compilers):\n");
  printf("0x4d2         = %d (hex)\n", 0x4d2);
  printf("0b10011010010 = %d (binary)\n", 0b10011010010);
  printf("02322         = %d (octal, prefix 0 - zero)\n", 0x4d2);
  return 0;
}

Output:

Decimal: 1234
Hex:     4d2
Octal:   2322
Literals (not supported by all compilers):
0x4d2         = 1234 (hex)
0b10011010010 = 1234 (binary)
02322         = 1234 (octal, prefix 0 - zero)

We don’t expect you to understand this code at this time. We will discuss C syntax, compilers, literals, etc. very soon. We also will not cover octal literals in this course; most standard C compilers will recognize hexadecimal and binary literals.

7Which base do we use?

Remember that there is only ever one number that can be represented in multiple ways. These are all the same number, thirty-two:

Different representations serve different purposes:

Above all, remember that computers operate in binary, but humans don’t. So it’s good to get more comfortable with converting between these representations before we move further.