Concept
How can you convert a decimal number to hexadecimal using modulo and floor?
Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk / Chapter 1
"So we know how to take a hexadecimal number (like 72E3_16) and find its decimal equivalent: we just interpret each place’s value as 1, 16, 256, 4096, and so on. What about going the other way? If we had a decimal number, how would we write its value hexadecimally? First, let’s learn two operations (if you don’t already know them) that come in handy when working with integers. The first is called the modulo operator (written “mod”), and simply gives the remainder when dividing two numbers. This is a concept you probably learned in elementary school but might not have used since then. As we get older (and use calculators), we tend to think of a division operation like 13 ÷ 3 as being 4.333.... But that’s when we want a real-valued (instead of integer-valued) answer. If we only want integers, then we say that 13 ÷ 3 is “4 with a remainder of 1.” (The “4” is called the quotient.) This means that if you have 13 objects, you can take four groups of 3’s out of them, and then have 1 object left over. The way we write this operation mathematically is “13 mod 3.” In this case, it turns out that 13 mod 3 = 1. Let’s think through what the mod operator yields for different values. We know that 13 mod 3 = 1. What about 14 mod 3? That is equal to 2, since we can (again) take out four groups of 3’s, but then we’d have two left over. What about 15 mod 3? That yields 0, since 3 goes in to 15 evenly, leaving no remainder at all. 16 mod 3 again gives us 1, just like 13 did. If you think it through, you’ll realize that 19 mod 3 will also be 1, as will 22 mod 3 and 25 mod 3. These numbers that give the same remainder are said to be “congruent mod 3.” The numbers 2, 5, 8, 11, 14, etc. are also all congruent (to each other) mod 3, since they all give remainder 2. Another observation is that the value of n mod k always gives a value between 0 and k − 1. We may not know at a glance what 407,332,117 mod 3 is, but we know it can’t be 12, or 4, or even 3, because if we had that many elements left after taking out groups of 3’s, we could still take out another group of 3. The remainder only gives us what’s left after taking out groups, so by definition there cannot be an entire group (or more) left in the remainder. The other operation we need is simply a “round down” operation, traditionally called “floor” and written with brackets: “⌊x⌋”. The floor of an integer is itself. The floor of a non-integer is the integer just below it. So ⌊7⌋ = 7 and ⌊4.81⌋ = 4. It’s that simple. The reason we use the floor operator is just to get the whole number of times one number goes into another. ⌊13 ÷ 3⌋ = 4, for example. By using mod and floor, we get the quotient and remainder of a division, both integers. If our numbers are 25 and 7, we have ⌊25 ÷ 7⌋ = 3 and 25 mod 7 = 4. Notice that this is equivalent to saying that 25 = 3 × 7 + 4. We’re asking “how many groups of 7 are in 25?” and the answer is that 25 is equal to 3 groups of 7, plus 4 extra. The general procedure for converting from one base to another is to repeatedly use mod and floor to strip out the digits from right to left. Here’s how you do it: Express a numeric value in a base. 1. Take the number mod the base. Write that digit down. 2. Divide the number by the base and take the floor: a) If you get zero, you’re done. b) If you get non-zero, then make this non-zero number your new value, move your pencil to the left of the digit(s) you’ve already written down, and return to step 1. As an example, let’s go backwards to the hex number 72E3 as in our example above, which we already computed was equal to 29,411 in decimal. Starting with 29,411, then, we follow our algorithm: 1. We first compute 29,411 mod 16. This turns out to be 3. So we write down 3. 2. We now divide 29,411 by 16 and take the floor. This produces ⌊29,411 ÷ 16⌋ = 1838. Since this is not zero, we make 1838 our new value, move our pencil to the left of the 3, and go back to step 1. 3. Now compute 1838 mod 16. This gives us the value 14, which is of course a base 10 number. The equivalent hex digit is E. So we now write down E to the left of the 3: E3. 4. Dividing 1838 by 16 and taking the floor gives us 114. Since this is again not zero, we make 114 our new value, move our pencil to the left of the E, and go back to step 1. 5. Next we compute 114 mod 16. This turns out to be 2, so we write down a 2: 2E3. 6. Computing ⌊114 ÷ 16⌋ produces 7, which is again not zero, so 7 becomes our new value. 7. 7 mod 16 is simply 7, so we write it down: 72E3. 8. Finally, ⌊7 ÷ 16⌋ is zero, so we’re done. The page has 72E3 written on it in big bold letters, which is the correct answer."
Related Ideas
- How do you convert between binary and decimal?Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk · Chapter 1
- How do you convert between binary and hexadecimal using nibbles?Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk · Chapter 1
- How are numbers interpreted in a base 7 system?Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk · Chapter 1
- How does a base determine the symbols and place values used to represent numbers?Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk · Chapter 1
- What happens after taking the floor of 24 divided by 2?Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk · Chapter 1
- What do you do after taking the floor of 6 divided by 2?Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk · Chapter 1
- What happens after dividing 3 by 2 and taking the floor?Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk · Chapter 1
- What value results from taking the floor of 12 divided by 2?Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk · Chapter 1