Binary and Hexadecimal

Binary

Computers process information in binary (base 2), which has only two digits: 0 and 1. In decimal (base 10) there are ten digits: 0–9. Both are positional systems, meaning a digit’s position determines its value. In binary we call these digits bits (binary digits).

Think of a computer memory as a whole bunch of electronic switches that are either off or on. When a switch is off, this represents the number 0, or the condition “false”. When a switch is on, this corresponds to the number 1 or the condition “true”. All the switches in computer memory then represent many 0s and 1s (or falses and trues).

Think about a light bulb, how many ‘states’ can it have? What about 2 Light Bulbs in a row?

A single switch has 2 states. Two switches together have 4 states:

  1. OFF, OFF → 0 (00)
  2. OFF, ON → 1 (01)
  3. ON, OFF → 2 (10)
  4. ON, ON → 3 (11)

Table of Values

Number of BitsNumber of ValuesRange
380–7
5320–31
82560–255

Think of each switch as a bit, and think about all the states you can cover. With 5 bits you can represent 32 numbers between 0 (00000) and 31 (11111). With k bits, the biggest number you can represent is always 2k -1. Another example: 8 bits = 28 = 256 possible values, 255 is largest number.

Positional Values

Binary place values are powers of 2, starting at 20 on the right:

26252423222120
6432168421

Example 1: 1001 (binary to decimal)

1×8 + 0×4 + 0×2 + 1×1 = 9 in decimal.

Example 2: 26 (decimal to binary)

26 = 16 + 8 + 2 → 11010 in binary.

Bits and Bytes

8 bits = 1 byte (B). This is the standard size for storing a single character. Larger units in computing are usually powers of 2. For this class, we use the software amount for prefixes, meaning the binary version rather than the decimal version. In the table below, kilo for us means 1024 rather than 1000.

PrefixDecimalBinary (Bytes)
kilo (k)103 = 1,000210 = 1,024
mega (M)106 = 1,000,000220 = 1,048,576
giga (G)109 = 1,000,000,000230 = 1,073,741,824

Example: 1 GB = (10243) × 8 = about 8.6×109 bits.

Hexadecimal

Binary is counting in base-2 (see textbook section Binary). Hexadecimal is counting in base-16. Decimal is counting in base-10. We use the subscript like X2 to indicate the number is written in base-2, or X10 for a base-10 number.

The symbols we use to count by powers are all values less than base:

  • Base 2 uses two symbols (0,1) to count.
  • Base 10 uses ten symbols (0,1,2,3,4,5,6,7,8,9) to count.
  • Base 16 uses symbols sixteen symbols: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f.

Base 10 Example

153410 = 1 x 103 + 5 x 10 2 + 3 x 10 1 + 4 x10 0 = 1000 + 500+ 30+ 4

Base 2 Example

11012 = 1x 23 + 1x 22 + 0x 21+ 1x 20 = 8 + 4+ 1 = 1310

Base 16 Examples

153416 = 1 x 16 3 + 5 x 162 + 3 x16 1 + 4x 160= 542810 

beef16 = b x 16 3 + e x 162 + e x16 1 + fx 160=  11 x 16 3 + 14 x 162 + 14 x16 1 + 15 x 160= 4887910

8 bits is a byte

A single hex digit can represent any four-digit binary value, or 4 bits: 

00002 = 016, 00012 = 116, …, 11102 = e16, 11112 = f16

Since a byte is eight bits, it needs two hex digits:

1f16 is the byte 000111112

This makes hex a convenient way to write colors, like we discuss when we talk about RGB values.

Hex digitBinaryDecimal
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
A101010
B101111
C110012
D110113
E111014
F111115
This table shows equivalent hex, binary, and decimal values

Pixels Represented in Hexadecimal

We specify pixel colors by the RGB Color Model (Red, Green, and Blue). An 8-bit number (one byte) represents the amount of each color from 0 to 255, the maximum intensity the monitor can provide. We usually write these colors in hex or as a sequence of decimal numbers. See more about pixels in the textbook section Colors.

Example

(42, 46, 163) is the (R,G,B) color also written as #2a2ea3 where 4210 = 2a16, 4610 = 2e16, and 16310 = a316

You need not write a subscript for RGB colors since it’s clear in context which base we’re talking about!

ASCII

The standard encoding of text inside a computer is called ASCII. ASCII is based on 7 bits, or 128 symbols (10 numbers, 26 letters of the English alphabet, some punctuation marks, etc.), and it represents visible characters or commands. Nowadays, ASCII is often extended to 8 bits.

You can type man ascii into a terminal to get a list of the codes. You should see a table with a header like this:

Oct   Dec   Hex   Char

with a table. One line is presented from the table here — we see that the hex value of the letter w is 77.

167   119   77    w

If you use the terminal on your personal machine, you may see a different formatting for the output.

Exercises

  1. Since we like to combine things in multiples of bits, we also use base-8 in computing; it’s called octal! Base-32 and Base-64 are even used, but more rarely — search RFC 4648 if you’re interested.
  2. How do the hex codes vary as you try different combinations in the RGB color picker?
  3. What are the pros and cons of using one base over another?
  4. Consider a base-64 number, which uses this set {A,B,…,Z,a,b,…,z,0,1,2,3,4,5,6,7,8,9,-,_} (26 upper and lower case letters, 0-9, a dash and underscore to get 64 symbols). The characters in the above set represent the numbers 0 through 63, in the same order—thus C represents 2, c represents 28, 3 represents 55, and _ represents 63. According to this schema, find the natural number whose base-64 expansion is d9WgXcQ, and find the base-64 expansion of the natural number 715984.
  5. Explore: 8 bits = 1 byte was not always the standard. What machines have thought in different numbers of bits? Can you find an example of a machine that works in 6 bits, or uses octal (groups of 3 bits)?
  6. Compare binary to ternary (base-3). See Quanta article.
  7. Why does writing a number x in base b require logb(x) digits (when you have no leading 0s)?