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.
How Base 10 Works
Each step to the left multiplies the bundle size by 10, so reading a Base 10 number like 243 just means adding up the bundles sitting in each slot:
100s[2]10s[4]1s[3]⟶(2×100)+(4×10)+(3×1)=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 instead of 10.
Binary (Base 2)
Text solution:
Drop the bits into the binary slots: Line up 11010 from right to left over the doubling box sizes (16s,8s,4s,2s,1s).
16s[1]8s[1]4s[0]2s[1]1s[0]
Add the boxes that are turned on: Sum the box sizes where the digit is 1 and skip the boxes with 0.
16+8+2=26
So the binary number 11010 represents the Base 10 quantity 26.
Whenever a binary slot reaches 2 bundles, they combine into 1 bundle of double the size in the next slot to the left, so adding 1 to a string of consecutive 1s 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 1101 and 1011 directly in binary without converting them to Base 10 first.
Adding in Binary
Text solution:
Add the 1s column and carry: In the 1s slot, 1+1=2 overflows into one bundle of 2, leaving 0 behind.
1+1=2⟶write 0, carry 1 to the 2s
Add the 2s and 4s columns with their carries: In both the 2s slot and the 4s slot, the carried 1 plus 1 plus 0 equals 2, which again writes 0 and carries 1 to the next slot left.
1+0+1=2⟶write 0, carry 1
Add the 8s column: The carried 1 plus 1 plus 1 makes 3 bundles of 8, so two of them combine into one 16 while one 8 stays behind.
1+1+1=3⟶write 1, carry 1 to the 16s⟶11000
So 1101+1011=11000, which matches 13+11=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 and find its binary digits.
Converting Base 10 to Binary
Text solution:
Method 1, Pack from the biggest box down (left to right): Subtract the largest binary box that fits inside 45(32), and keep subtracting the largest boxes that fit into what remains (8,4, and 1).
Method 2, Divide by 2 to pull out slots (right to left): Divide by 2 at each step so the remainder gives the current 1s slot and the quotient shifts all larger boxes one step to the right.
45÷222÷211÷25÷22÷21÷2=22 R 1(1s place)=11 R 0(2s place)=5 R 1(4s place)=2 R 1(8s place)=1 R 0(16s place)=0 R 1(32s place)
Read the remainders from bottom to top: Collect the remainders from the 32s slot back down to the 1s slot.
45=101101
5
Why Every Base-b Expansion Exists and Is Unique
Both methods gave the exact same binary string for 45, which raises a basic question for any integer base b≥2: how do we know that every positive integer n can be packed into base-b boxes using only digits from 0 to 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 by b≥2 gives one and only one quotient q and remainder r satisfying
n=q⋅b+r,0≤r<b
Because 0≤r<b, the remainder r is guaranteed to be a valid base-b digit from 0 to b−1, and whenever q>0, dividing by b≥2 makes q<n so Strong Induction applies to q.
Why Every Base-b Expansion Exists and Is Unique
Text solution:
Existence (Strong Induction on n): Divide n by b to put the remainder r in the 1s slot; if q=0,n=r is already a single base-b digit, and if q>0, Strong Induction gives a base-b expansion for q<n whose boxes all shift one slot to the left when multiplied by b.
n=q⋅b+r,0≤r<b
Uniqueness (Factoring out b): Factor b out of every slot to the left of the 1s place so the expansion matches the Division Algorithm.
n0=q(d3b2+d2b+d1)⋅b+d0,≤d0<b
Because the quotient and remainder are unique, the rightmost digit d0 must equal r and the remaining slots must total q, whose own digits are fixed by Strong Induction when q>0 (when q=0, those slots are all empty).
In any base b, filling every slot smaller than a given box to its maximum digit b−1 always stops 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, we can convert between Base 10 and any base, such as Base 5, where slots hold digits 0 through 4 and box sizes multiply by 5 each step to the left (125s,25s,5s,1s).
Working in Any Base
We write the subscript 5 in 3425 to label the base of the boxes so it isn't misread as three hundred forty-two in Base 10.
Text solution:
Base 5 to Base 10 (add the boxes): Drop the digits of 3425 into the 25s,5s, and 1s boxes and sum.
3425=(3×25)+(4×5)+(2×1)=75+20+2=97
Base 10 to Base 5 (divide by 5): Divide 143 by 5 repeatedly to find the remainders from the 1s place up to the 125s place, then read from bottom to top.
143÷528÷55÷51÷5⟶143=28 R 3(1s place)=5 R 3(5s place)=1 R 0(25s place)=0 R 1(125s place)=10335
7
Hexadecimal (Base 16)
When a base is larger than 10, like Hexadecimal (Base 16), where a single slot holds up to 15 items before overflowing at 16, writing 15 inside one slot would look like two slots (1 and 5), so we borrow the letters `A` through `F` as single-character digits for 10 through 15:
A10B11C12D13E14F15
Hexadecimal (Base 16)
Text solution:
Hexadecimal to Base 10 (add the Base 16 boxes): Replace F with 15, multiply each digit of 2F316 by its Base 16 box size (256s,16s,1s), and add.
2F316=(2×256)+(F15×16)+(3×1)=512+240+3=755
Base 10 to Hexadecimal (divide by 16): Divide 428 by 16 repeatedly, write any remainder from 10 to 15 as a single hex letter, and read from bottom to top.
428÷1626÷161÷16⟶428=26 R 12⟶C(1s place)=1 R 10⟶A(16s place)=0 R 1⟶1(256s place)=1AC16
8
Shortcuts Between Binary, Hexadecimal, and Octal
Converting a long binary string to Base 10 is tedious because 10 is not a power of 2, so Base 10 boxes don't line up with binary boxes. In contrast, 16=24 and 8=23 are powers of 2, which means every 4 binary slots pack into one exact Hexadecimal slot and every 3 binary slots pack into one exact Octal (Base 8) slot.
Shortcuts Between Binary, Hexadecimal, and Octal
Text solution:
Binary to Hexadecimal (group in 4s from the right): Split 11010110101110012 into 4-bit (8,4,2,1) boxes starting at the 1s place and convert each box to a single hex digit.
Hexadecimal to Binary (expand each digit to 4 bits): Write each digit of 3E816 as its own 4-bit (8,4,2,1) group and drop leading zeros on the far left.
001131110E10008⟶0011111010002=11111010002
Binary to Octal (group in 3s from the right): Split 110101102 into 3-bit (4,2,1) boxes from the right, padding the front group with a 0.
0110101102+14+2=3=2=6
⟶3268
Octal to Binary (expand each digit to 3 bits): Write each digit of 7458 as its own 3-bit (4,2,1) group.
111710041015⟶1111001012
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 4-bit or 3-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 uses the digits 0 through b−1 (borrowing A=10 through F=15 in Base 16) in slots whose box sizes start at 1 on the right and multiply by b each step to the left (1,b,b2,b3,…). Filling every slot below bk with the maximum digit b−1 always totals bk−1, one short of the next box up.
To convert from base b to Base 10, multiply each digit by its box size and add them up (3425=3×25+4×5+2×1=97). To convert from Base 10 to base b, either subtract the largest base-b boxes that fit from left to right, or divide repeatedly by b(n=q⋅b+r with 0≤r<b) so each remainder gives the next digit from right to left (reading bottom to top: 45→1011012).
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), and Hexadecimal (16=24): instead, group binary bits from the right into 3-bit (4,2,1) boxes for Octal (011∣010∣1102=3268) or 4-bit (8,4,2,1) boxes for Hexadecimal (1101∣10012=D916), and convert between Hexadecimal and Octal by expanding into binary first.
9
Practice
1
Convert the binary number 10110112 to Base 10.
Make sure the rightmost slot is labeled 1 (not 2, which doubles the entire answer to 182) and count all seven slots including the two 0s so the leftmost box is 64 (skipping a 0 slot gives 59).
Write the seven doubling box sizes under the digits starting at 1 on the right, then add the boxes that are turned on. Watch out: Make sure the rightmost slot is labeled 1 (not 2, which doubles the entire answer to 182) and count all seven slots including the two 0s so the leftmost box is 64 (skipping a 0 slot gives 59).
64+16+8+2+1=91
2
Convert the Base 10 number 83 to binary.
Subtract the largest binary box that fits inside 83, or divide 83 by 2 repeatedly and track the remainders. Watch out: Put a 0 in every skipped box (32s,8s,4s), and if you divide by 2, read the remainders from bottom to top; reading from top to bottom reverses the bits to 11001012(101).
10100112
3
Add 101112+110112 directly in binary, showing the result in binary.
Line up the five slots from right to left and carry 1 to the next column whenever a column sum reaches 2 or 3.Watch out: In the 2s and 16s columns, adding 1+1 plus a carried 1 gives 3, which writes 1 and carries 1 (writing 0 and carrying 1 as if the sum were 2 gives 1000002).
1100102
4
Convert the Base 5 number 4135 into Base 7 by first converting to Base 10.
Convert 4135 to 108 in Base 10 before dividing by 7 (dividing 413 directly by 7 treats it as Base 10 and gives 11307), and read the Base 7 remainders from bottom to top (reading top to bottom gives 3127).
Find the Base 10 total using the Base 5 boxes (25s,5s,1s), then divide that Base 10 number by 7 repeatedly. Watch out: Convert 4135 to 108 in Base 10 before dividing by 7 (dividing 413 directly by 7 treats it as Base 10 and gives 11307), and read the Base 7 remainders from bottom to top (reading top to bottom gives 3127).
2137
5
Convert the hexadecimal number 3D916 to Base 10.
Remember that A starts at 10, so D is 13 (starting A at 11 makes D=14 and gives 1001).
Replace D with its number from 10 to 15, then multiply each digit by its Base 16 box size (256s,16s,1s). Watch out: Remember that A starts at 10, so D is 13 (starting A at 11 makes D=14 and gives 1001).
768+208+9=985
6
Convert the Base 10 number 684 to hexadecimal.
Write the remainders 12 and 10 as single hex digits C and A rather than two-digit numbers (which would look like 2101216), and read from bottom to top rather than CA216.
Divide 684 by 16 repeatedly and turn each remainder from 10 to 15 into a single hex letter. Watch out: Write the remainders 12 and 10 as single hex digits C and A rather than two-digit numbers (which would look like 2101216), and read from bottom to top rather than CA216.
2AC16
7
Convert the 10-bit binary number 10111011012 directly to hexadecimal and to octal without going through Base 10.
Always start grouping from the right; grouping from the left misaligns the boxes and gives BB416 and 56648.
Group the bits starting from the rightmost 1s bit, into fours (8,4,2,1) for hexadecimal and into threes (4,2,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 and 56648.
2ED16 and 13558
8
Convert the hexadecimal number B616 directly to octal by converting to binary first.
Keep the leading zero when expanding 6 into four bits as 01102; writing 6 as just three bits (1102) drops a slot and gives 1368.
Expand each hex digit into its own 4-bit (8,4,2,1) binary group, then regroup the bits into threes (4,2,1) from the right. Watch out: Keep the leading zero when expanding 6 into four bits as 01102; writing 6 as just three bits (1102) drops a slot and gives 1368.
2668
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?
A Base 5 slot overflows at 5, so the maximum digit is 4 (not 5), and filling the first four slots stops 1 short of 54=625 (giving 624, not 625).
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). Watch out: A Base 5 slot overflows at 5, so the maximum digit is 4 (not 5), and filling the first four slots stops 1 short of 54=625 (giving 624, not 625).
44445, which is 624
10
A positive integer n has the hexadecimal expansion 4A7F16. Without converting n to Base 10, find the quotient q (in hexadecimal) and remainder r (in Base 10) when n is divided by 16, and when n is divided by 256.
Every slot to the left of the 1s place is a multiple of 16, and every slot to the left of the 16s place is a multiple of 256=162.Watch out: When dividing by 256, both the 16s and 1s slots (7F16=7×16+15) stay behind as the remainder; taking only the single digit 7 or 15 misses the other slot.
Divided by 16:q=4A716 and r=15; divided by 256:q=4A16 and r=127