Base and Radix
What is this?
A "base" (or radix) is simply how many distinct digits a number system uses before it rolls over to the next place. We count in base 10 with digits 0-9, computers love base 2 (just 0 and 1) and base 16, and even spreadsheet column letters are a quirky base 26. These problems convert a number from one base to another by either peeling off digits one at a time or folding them back together.
๐ก Fun fact: Spreadsheet column headers (A, B, ..., Z, AA, AB) are a base-26 system with no zero digit, which is why the math needs a sneaky "subtract one" step that trips up almost everyone the first time.
๐ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: every positional number is a sum of digit times radix-power, so you change bases by repeatedly dividing to peel off the least-significant digit, or repeatedly multiplying to fold digits back into a value.
The pattern
Base conversion is two mirror-image loops. To go from an integer to a string in base b, divide by b over and over: each remainder is the next digit (low to high), and the quotient feeds the next step until it reaches zero. To go from a string back to an integer, walk the characters left to right, multiplying the running total by b and adding the current digit โ Horner's method.
Two twists separate the easy cases from the interview-worthy ones. First, two's complement: negative integers have no minus sign in their bit pattern, so a hex conversion must treat the value as unsigned bits, masking four bits at a time and shifting right logically, not arithmetically. Second, bijective numbering: spreadsheet columns run A, B, ..., Z, AA, with no zero digit, so the usual divide-by-radix loop needs an off-by-one correction (subtract one before each division) to map between the integer and its title.
The problems
Convert a Number to Hexadecimal takes the raw 32-bit pattern and emits base-16. Mask the low four bits, map them to a hex character, shift right by four, and repeat until the value is zero; using unsigned shifts makes negatives fall out of the same loop without special-casing.
Excel Sheet Column Number is the multiply-and-accumulate direction: read "ZY" left to right, each letter contributing its 1-to-26 value as you scale the total by 26. This is plain base-26 with digits offset to start at one.
Excel Sheet Column Title is the inverse divide loop, but bijective. Because there is no zero digit, you decrement by one before taking the remainder and quotient on each step, then prepend the resulting letter. Forgetting that single subtraction is the classic bug.
Key takeaways
- Divide-by-radix peels digits off; multiply-by-radix builds value up โ pick the direction the problem asks for.
- Two's complement means hex/binary conversions act on bits, so mask and shift logically instead of testing for a sign.
- Bijective bases (no zero) need a minus-one correction inside the divide loop; standard bases do not.
- Both directions run in time proportional to the number of digits, with constant extra space.
Core idea: A hex digit is a group of 4 bits (a nibble), so peel off the number 4 bits at a time โ and for negatives, mask to 32 bits first so the exact same loop just works.
Problem, rephrased
You're writing the "memory inspector" panel for a debugger. The user clicks a 32-bit register and you need to show its value the way every debugger does: as a lowercase hexadecimal string. Positive values are the easy part. The catch is that registers hold raw bit patterns โ a value of -1 isn't displayed as -1, it's displayed as the 32-bit pattern that encodes -1 in two's complement, which is ffffffff.
So: given a 32-bit signed integer, return its hexadecimal string with these rules.
- Lowercase letters (
aโf), never uppercase. - No leading zeros โ except the number
0itself, which is the single character"0". - Negative numbers use their 32-bit two's-complement bit pattern.
- You may not call any built-in that does base conversion for you (no
hex(), noformat(n, 'x')).
| Input (decimal) | Output (hex) | Why |
|---|---|---|
0 |
"0" |
special-cased single zero |
26 |
"1a" |
1*16 + 10 |
255 |
"ff" |
one full byte set |
-1 |
"ffffffff" |
all 32 bits set |
2147483647 |
"7fffffff" |
INT_MAX |
-2147483648 |
"80000000" |
INT_MIN, only the sign bit |
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson