Discrete MathematicsDivisibility, Primes and Greatest Common Divisors2 of 6
Divisibility, Primes and Greatest Common Divisors · 2

Binary, Hexadecimal and Changing Bases

A number is a quantity of things, and we need a way to write any quantity down using only a handful of symbols.

1

How Base 10 Works

In everyday arithmetic, we only have ten symbols (`0` through `9`), so we count larger piles by grouping items into slots that overflow at 10\base{10}.

How Base 10 Works
25\htmlClass{lc-green}{25}

Each step to the left multiplies the bundle size by 10\base{10}, so reading a Base 10 number like 243\total{243} just means adding up the bundles sitting in each slot:

100s10s1s[2][4][3]⟶(2×100)+(4×10)+(3×1)=243\displaystyle \begin{array}{ccc} \boxsize{100\text{s}} & \boxsize{10\text{s}} & \boxsize{1\text{s}} \\ [2] & [4] & [3] \end{array} \quad \longrightarrow \quad (2 \times \boxsize{100}) + (4 \times \boxsize{10}) + (3 \times \boxsize{1}) = \total{243}
2

Binary (Base 2)

A computer wire is easiest to build when it only has two states, off or on, which leaves us with just two symbols, `0` and `1`, and slots that overflow at 2\base{2} instead of 10\base{10}.

Base 10\text{Base }\htmlClass{lc-violet}{10}
1s\htmlClass{lc-blue}{1\text{s}}
×10\htmlClass{lc-violet}{\times 10}
10s\htmlClass{lc-blue}{10\text{s}}
×10\htmlClass{lc-violet}{\times 10}
100s\htmlClass{lc-blue}{100\text{s}}
×10\htmlClass{lc-violet}{\times 10}
1000s\htmlClass{lc-blue}{1000\text{s}}
Base 2\text{Base }\htmlClass{lc-violet}{2}
1s\htmlClass{lc-blue}{1\text{s}}
×2\htmlClass{lc-violet}{\times 2}
2s\htmlClass{lc-blue}{2\text{s}}
×2\htmlClass{lc-violet}{\times 2}
4s\htmlClass{lc-blue}{4\text{s}}
×2\htmlClass{lc-violet}{\times 2}
8s\htmlClass{lc-blue}{8\text{s}}
×2\htmlClass{lc-violet}{\times 2}
16s\htmlClass{lc-blue}{16\text{s}}
×2\htmlClass{lc-violet}{\times 2}
32s\htmlClass{lc-blue}{32\text{s}}
Binary (Base 2)
01234567890\enspace 1\enspace 2\enspace 3\enspace 4\enspace 5\enspace 6\enspace 7\enspace 8\enspace 9
22
44
33
1s\htmlClass{lc-blue}{1\text{s}}
×10\htmlClass{lc-violet}{\times 10}
10s\htmlClass{lc-blue}{10\text{s}}
×10\htmlClass{lc-violet}{\times 10}
100s\htmlClass{lc-blue}{100\text{s}}
×10\htmlClass{lc-violet}{\times 10}
1000s\htmlClass{lc-blue}{1000\text{s}}

Text solution:

So the binary number 1101011010 represents the Base 10 quantity 26\total{26}.

Whenever a binary slot reaches 2\base{2} bundles, they combine into 1\carry{1} bundle of double the size in the next slot to the left, so adding 1\carry{1} to a string of consecutive 1s1\text{s} sets off a chain reaction of overflows that rolls `...0111` over to `...1000`.

Whenever a slot gets a second bundle, the pair combines into one bundle of double the size in the next slot to the left, and this repeats until a bundle lands in an empty slot.
3

Adding in Binary

We want to add two binary numbers like 11011101 and 10111011 directly in binary without converting them to Base 10 first.

Adding in Binary
Base 10\text{Base }\htmlClass{lc-violet}{10}
10s1s\htmlClass{lc-blue}{10\text{s}\hspace{0.839em}1\text{s}}
77
++
55
Base 2\text{Base }\htmlClass{lc-violet}{2}
16s8s4s2s1s\htmlClass{lc-blue}{16\text{s}\hspace{0.839em}8\text{s}\hspace{1.089em}4\text{s}\hspace{1.089em}2\text{s}\hspace{1.089em}1\text{s}}
11011\hspace{0.852em}1\hspace{0.852em}0\hspace{0.852em}1
++
10111\hspace{0.852em}0\hspace{0.852em}1\hspace{0.852em}1

Text solution:

So 1101+1011=110001101 + 1011 = 11000, which matches 13+11=24\total{13} + \total{11} = \total{24} in Base 10.

4

Converting Base 10 to Binary

Now we want to go in the opposite direction: take a Base 10 number like 45\total{45} and find its binary digits.

Converting Base 10 to Binary
45\htmlClass{lc-green}{45}
1s\htmlClass{lc-blue}{1\text{s}}
2s\htmlClass{lc-blue}{2\text{s}}
4s\htmlClass{lc-blue}{4\text{s}}
8s\htmlClass{lc-blue}{8\text{s}}
16s\htmlClass{lc-blue}{16\text{s}}
32s\htmlClass{lc-blue}{32\text{s}}
64s\htmlClass{lc-blue}{64\text{s}}

Text solution:

5

Why Every Base-bb Expansion Exists and Is Unique

Both methods gave the exact same binary string for 45\total{45}, which raises a basic question for any integer base b≥2\base{b} \ge 2: how do we know that every positive integer n\total{n} can be packed into base-b\base{b} boxes using only digits from 00 to b−1\base{b} - 1, and that no second combination of digits can ever give the same total?

Recall from the Division Algorithm that dividing any integer n\total{n} by b≥2\base{b} \ge 2 gives one and only one quotient qq and remainder r\remainder{r} satisfying

n\displaystyle \total{n} =q⋅b+r,0\displaystyle = q \cdot \base{b} + \remainder{r}, \qquad 0 ≤r<b\displaystyle \le \remainder{r} < \base{b}

Because 0≤r<b0 \le \remainder{r} < \base{b}, the remainder r\remainder{r} is guaranteed to be a valid base-b\base{b} digit from 00 to b−1\base{b} - 1, and whenever q>0q > 0, dividing by b≥2\base{b} \ge 2 makes q<nq < \total{n} so Strong Induction applies to qq.

Why Every Base-b Expansion Exists and Is Unique
1. Exists?1.\ \text{Exists?}
2. Unique?2.\ \text{Unique?}
n\htmlClass{lc-green}{n}
1s\htmlClass{lc-blue}{1\text{s}}
bs\htmlClass{lc-blue}{b\text{s}}
b2s\htmlClass{lc-blue}{b^2\text{s}}
b3s\htmlClass{lc-blue}{b^3\text{s}}
digits 0 to b−1\text{digits } 0 \text{ to } \htmlClass{lc-violet}{b} - 1

Text solution:

In any base b\base{b}, filling every slot smaller than a given box to its maximum digit b−1\base{b} - 1 always stops 1\remainder{1} dot short of that box's size, so skipping a needed box leaves dots that can never fit into the smaller slots.

Filling all the smaller slots to their limit always stops one dot short of the next box, so a pile that needs a larger box can never fit in the smaller ones.
6

Working in Any Base

Because the slots work the same way for every base b≥2\base{b} \ge 2, we can convert between Base 10 and any base, such as Base 5, where slots hold digits 00 through 44 and box sizes multiply by 5\base{5} each step to the left (125s,25s,5s,1s\boxsize{125\text{s}}, \boxsize{25\text{s}}, \boxsize{5\text{s}}, \boxsize{1\text{s}}).

Working in Any Base
012340\enspace 1\enspace 2\enspace 3\enspace 4
Base 5\text{Base }\htmlClass{lc-violet}{5}
1s\htmlClass{lc-blue}{1\text{s}}
×5\htmlClass{lc-violet}{\times 5}
5s\htmlClass{lc-blue}{5\text{s}}
×5\htmlClass{lc-violet}{\times 5}
25s\htmlClass{lc-blue}{25\text{s}}
×5\htmlClass{lc-violet}{\times 5}
125s\htmlClass{lc-blue}{125\text{s}}
3425= ?342_{\htmlClass{lc-violet}{5}} = \ ?

We write the subscript 5\base{5} in 3425342_{\base{5}} to label the base of the boxes so it isn't misread as three hundred forty-two in Base 10.

Text solution:

7

Hexadecimal (Base 16)

When a base is larger than 10\base{10}, like Hexadecimal (Base 16), where a single slot holds up to 1515 items before overflowing at 16\base{16}, writing 1515 inside one slot would look like two slots (11 and 55), so we borrow the letters `A` through `F` as single-character digits for 1010 through 1515:

ABCDEF101112131415\displaystyle \begin{array}{cccccc} \text{A} & \text{B} & \text{C} & \text{D} & \text{E} & \text{F} \\ 10 & 11 & 12 & 13 & 14 & 15 \end{array}
Hexadecimal (Base 16)
a slot overflows at 16\text{a slot overflows at }\htmlClass{lc-violet}{16}
1515
1s\htmlClass{lc-blue}{1\text{s}}

Text solution:

8

Shortcuts Between Binary, Hexadecimal, and Octal

Converting a long binary string to Base 10 is tedious because 10\base{10} is not a power of 2\base{2}, so Base 10 boxes don't line up with binary boxes. In contrast, 16=24\base{16} = \boxsize{2^4} and 8=23\base{8} = \boxsize{2^3} are powers of 2\base{2}, which means every 44 binary slots pack into one exact Hexadecimal slot and every 33 binary slots pack into one exact Octal (Base 8) slot.

Hexadecimal (Base 16)\text{Hexadecimal (Base }\htmlClass{lc-violet}{16}\text{)}
1\htmlClass{lc-blue}{1}
2\htmlClass{lc-blue}{2}
4\htmlClass{lc-blue}{4}
8\htmlClass{lc-blue}{8}
1s\htmlClass{lc-blue}{1\text{s}}
2s\htmlClass{lc-blue}{2\text{s}}
4s\htmlClass{lc-blue}{4\text{s}}
8s\htmlClass{lc-blue}{8\text{s}}
1s\htmlClass{lc-blue}{1\text{s}}
1\htmlClass{lc-blue}{1}
2\htmlClass{lc-blue}{2}
4\htmlClass{lc-blue}{4}
8\htmlClass{lc-blue}{8}
16s\htmlClass{lc-blue}{16\text{s}}
32s\htmlClass{lc-blue}{32\text{s}}
64s\htmlClass{lc-blue}{64\text{s}}
128s\htmlClass{lc-blue}{128\text{s}}
16s\htmlClass{lc-blue}{16\text{s}}
Octal (Base 8)\text{Octal (Base }\htmlClass{lc-violet}{8}\text{)}
1\htmlClass{lc-blue}{1}
2\htmlClass{lc-blue}{2}
4\htmlClass{lc-blue}{4}
1s\htmlClass{lc-blue}{1\text{s}}
2s\htmlClass{lc-blue}{2\text{s}}
4s\htmlClass{lc-blue}{4\text{s}}
1s\htmlClass{lc-blue}{1\text{s}}
1\htmlClass{lc-blue}{1}
2\htmlClass{lc-blue}{2}
4\htmlClass{lc-blue}{4}
8s\htmlClass{lc-blue}{8\text{s}}
16s\htmlClass{lc-blue}{16\text{s}}
32s\htmlClass{lc-blue}{32\text{s}}
8s\htmlClass{lc-blue}{8\text{s}}
Shortcuts Between Binary, Hexadecimal, and Octal
010\quad 1
1 1 0 1 0 1 1 0 1 0 1 1 1 0 0 11\,1\,0\,1\,0\,1\,1\,0\,1\,0\,1\,1\,1\,0\,0\,1
Base 16Base 8Base 10\text{Base }\htmlClass{lc-violet}{16}\qquad\text{Base }\htmlClass{lc-violet}{8}\qquad\text{Base }\htmlClass{lc-violet}{10}

Text solution:

Flipping a single bit can change every digit of the Base 10 total at once, but it only ever changes the one Hexadecimal digit and the one Octal digit whose 44-bit or 33-bit group holds that bit.

Flipping a single bit can change every Base 10 digit at once, but it only ever changes the one Hex digit and one Octal digit whose group holds that bit.

Key concepts

Every base b≥2\base{b} \ge 2 uses the digits 00 through b−1\base{b} - 1 (borrowing A=10\text{A}=10 through F=15\text{F}=15 in Base 16) in slots whose box sizes start at 1\boxsize{1} on the right and multiply by b\base{b} each step to the left (1,b,b2,b3,…\boxsize{1}, \boxsize{b}, \boxsize{b^2}, \boxsize{b^3}, \dots). Filling every slot below bk\boxsize{b^k} with the maximum digit b−1\base{b} - 1 always totals bk−1\boxsize{b^k} \remainder{- 1}, one short of the next box up.

To convert from base b\base{b} to Base 10, multiply each digit by its box size and add them up (3425=3×25+4×5+2×1=97342_{\base{5}} = 3 \times \boxsize{25} + 4 \times \boxsize{5} + 2 \times \boxsize{1} = \total{97}). To convert from Base 10 to base b\base{b}, either subtract the largest base-b\base{b} boxes that fit from left to right, or divide repeatedly by b\base{b} (n=q⋅b+r\total{n} = q \cdot \base{b} + \remainder{r} with 0≤r<b0 \le \remainder{r} < \base{b}) so each remainder gives the next digit from right to left (reading bottom to top: 45→1011012\total{45} \to 101101_{\base{2}}).

When converting between two bases whose box sizes don't align (like Base 5 and Base 7), convert to Base 10 first. Do not go through Base 10 when converting between Binary, Octal (8=23\base{8} = \boxsize{2^3}), and Hexadecimal (16=24\base{16} = \boxsize{2^4}): instead, group binary bits from the right into 33-bit (4,2,1)(\boxsize{4}, \boxsize{2}, \boxsize{1}) boxes for Octal (011∣010∣1102=3268011\mid 010\mid 110_{\base{2}} = 326_{\base{8}}) or 44-bit (8,4,2,1)(\boxsize{8}, \boxsize{4}, \boxsize{2}, \boxsize{1}) boxes for Hexadecimal (1101∣10012=D9161101\mid 1001_{\base{2}} = \text{D}9_{\base{16}}), and convert between Hexadecimal and Octal by expanding into binary first.

9

Practice

1

Convert the binary number 101101121011011_{\base{2}} to Base 10.

Write the seven doubling box sizes under the digits starting at 1\boxsize{1} on the right, then add the boxes that are turned on. Watch out: Make sure the rightmost slot is labeled 1\boxsize{1} (not 2\boxsize{2}, which doubles the entire answer to 182182) and count all seven slots including the two 0s0\text{s} so the leftmost box is 64\boxsize{64} (skipping a 00 slot gives 5959).

64+16+8+2+1=91\boxsize{64} + \boxsize{16} + \boxsize{8} + \boxsize{2} + \boxsize{1} = 91
2

Convert the Base 10 number 83\total{83} to binary.

Subtract the largest binary box that fits inside 83\total{83}, or divide 83\total{83} by 2\base{2} repeatedly and track the remainders. Watch out: Put a 00 in every skipped box (32s,8s,4s\boxsize{32\text{s}}, \boxsize{8\text{s}}, \boxsize{4\text{s}}), and if you divide by 2\base{2}, read the remainders from bottom to top; reading from top to bottom reverses the bits to 110010121100101_{\base{2}} (101101).

101001121010011_{\base{2}}
3

Add 101112+11011210111_{\base{2}} + 11011_{\base{2}} directly in binary, showing the result in binary.

Line up the five slots from right to left and carry 1\carry{1} to the next column whenever a column sum reaches 2\total{2} or 3\total{3}. Watch out: In the 2s\boxsize{2\text{s}} and 16s\boxsize{16\text{s}} columns, adding 1+11 + 1 plus a carried 1\carry{1} gives 3\total{3}, which writes 11 and carries 1\carry{1} (writing 00 and carrying 1\carry{1} as if the sum were 22 gives 1000002100000_{\base{2}}).

1100102110010_{\base{2}}
4

Convert the Base 5 number 4135413_{\base{5}} into Base 7 by first converting to Base 10.

Find the Base 10 total using the Base 5 boxes (25s,5s,1s\boxsize{25\text{s}}, \boxsize{5\text{s}}, \boxsize{1\text{s}}), then divide that Base 10 number by 7\base{7} repeatedly. Watch out: Convert 4135413_{\base{5}} to 108\total{108} in Base 10 before dividing by 7\base{7} (dividing 413413 directly by 7\base{7} treats it as Base 10 and gives 113071130_{\base{7}}), and read the Base 7 remainders from bottom to top (reading top to bottom gives 3127312_{\base{7}}).

2137213_{\base{7}}
5

Convert the hexadecimal number 3D9163\text{D}9_{\base{16}} to Base 10.

Replace D\text{D} with its number from 1010 to 1515, then multiply each digit by its Base 16 box size (256s,16s,1s\boxsize{256\text{s}}, \boxsize{16\text{s}}, \boxsize{1\text{s}}). Watch out: Remember that A\text{A} starts at 1010, so D\text{D} is 1313 (starting A\text{A} at 1111 makes D=14\text{D} = 14 and gives 10011001).

768+208+9=985768 + 208 + 9 = 985
6

Convert the Base 10 number 684\total{684} to hexadecimal.

Divide 684\total{684} by 16\base{16} repeatedly and turn each remainder from 1010 to 1515 into a single hex letter. Watch out: Write the remainders 12\remainder{12} and 10\remainder{10} as single hex digits C\text{C} and A\text{A} rather than two-digit numbers (which would look like 210121621012_{\base{16}}), and read from bottom to top rather than CA216\text{CA}2_{\base{16}}.

2AC162\text{AC}_{\base{16}}
7

Convert the 10-bit binary number 101110110121011101101_{\base{2}} directly to hexadecimal and to octal without going through Base 10.

Group the bits starting from the rightmost 1s\boxsize{1\text{s}} bit, into fours (8,4,2,1)(\boxsize{8}, \boxsize{4}, \boxsize{2}, \boxsize{1}) for hexadecimal and into threes (4,2,1)(\boxsize{4}, \boxsize{2}, \boxsize{1}) for octal, and pad the leftmost group with leading zeros. Watch out: Always start grouping from the right; grouping from the left misaligns the boxes and gives BB416\text{BB}4_{\base{16}} and 566485664_{\base{8}}.

2ED162\text{ED}_{\base{16}} and 135581355_{\base{8}}
8

Convert the hexadecimal number B616\text{B}6_{\base{16}} directly to octal by converting to binary first.

Expand each hex digit into its own 44-bit (8,4,2,1)(\boxsize{8}, \boxsize{4}, \boxsize{2}, \boxsize{1}) binary group, then regroup the bits into threes (4,2,1)(\boxsize{4}, \boxsize{2}, \boxsize{1}) from the right. Watch out: Keep the leading zero when expanding 66 into four bits as 011020110_{\base{2}}; writing 66 as just three bits (1102110_{\base{2}}) drops a slot and gives 1368136_{\base{8}}.

2668266_{\base{8}}
9

What is the largest Base 10 number that can be written using at most 4 digits in Base 5, and what is its Base 5 expansion?

Fill all four Base 5 slots with the largest valid Base 5 digit, then compare that pile to the size of the fifth slot (54\boxsize{5^4}). Watch out: A Base 5 slot overflows at 5\base{5}, so the maximum digit is 44 (not 55), and filling the first four slots stops 1\remainder{1} short of 54=625\boxsize{5^4} = \boxsize{625} (giving 624624, not 625625).

444454444_{\base{5}}, which is 624624
10

A positive integer n\total{n} has the hexadecimal expansion 4A7F164\text{A}7\text{F}_{\base{16}}. Without converting n\total{n} to Base 10, find the quotient qq (in hexadecimal) and remainder r\remainder{r} (in Base 10) when n\total{n} is divided by 16\base{16}, and when n\total{n} is divided by 256\base{256}.

Every slot to the left of the 1s\boxsize{1\text{s}} place is a multiple of 16\base{16}, and every slot to the left of the 16s\boxsize{16\text{s}} place is a multiple of 256=162\base{256} = \boxsize{16^2}. Watch out: When dividing by 256\base{256}, both the 16s\boxsize{16\text{s}} and 1s\boxsize{1\text{s}} slots (7F16=7×16+15\remainder{7\text{F}}_{\base{16}} = 7 \times \boxsize{16} + 15) stay behind as the remainder; taking only the single digit 77 or 1515 misses the other slot.

Divided by 16\base{16}: q=4A716q = 4\text{A}7_{\base{16}} and r=15\remainder{r} = 15; divided by 256\base{256}: q=4A16q = 4\text{A}_{\base{16}} and r=127\remainder{r} = 127