Mental Math for Programmers: Binary, Hex, and Powers of Two

Programmers hit the same mental math wall every day. What is 2^20 in millions, how many bits does a 10-digit ID need, what does 0x7F mean in decimal, and is this O(n log n) algorithm fast enough for a million rows. A calculator breaks flow, a mental model saves five seconds per lookup across thousands of lookups per year. This guide gives you the five primitives that cover 95% of programmer arithmetic, each drilled with real examples from memory sizing, IP math, hex color codes, and complexity analysis.
Powers of Two Every Programmer Should Memorize
Memorize the first twenty powers of two and three-quarters of programming math collapses to a lookup. The pattern is a doubling ladder, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, and onward, each term twice the previous. The practical anchor points are 2^10 ≈ 1,000 (kilo), 2^20 ≈ 1,000,000 (mega), 2^30 ≈ 1,000,000,000 (giga), 2^40 ≈ 1 trillion (tera). The error on each anchor is about 2.4%, cumulative, which matters for rough estimation and not for sizing a 2 GB RAM allocation. For intermediate values, double or halve from the nearest anchor: 2^17 is 2^20 divided by eight, roughly 131,000.
- 2^8 = 256 (byte range, color channel, one UTF-8 code unit)
- 2^10 = 1,024 (kibibyte, roughly one thousand)
- 2^16 = 65,536 (short int range, UTF-16 BMP, TCP port count)
- 2^20 ≈ 1.05 million (mebibyte, roughly one million)
- 2^24 = 16,777,216 (24-bit color space, true-color depth)
- 2^30 ≈ 1.07 billion (gibibyte, roughly one billion)
- 2^32 ≈ 4.29 billion (unsigned 32-bit max, IPv4 address space)
Each line maps directly to a real limit: port numbers run 0 to 65535, a signed 32-bit int tops at 2,147,483,647, a 24-bit color field holds 16.7 million distinct shades. The drill is to pick any power, say 2^18, and answer in under three seconds (262,144). Our powers of two ladder breaks the memorisation into a three-day plan with spaced recall and a self-check script.
Decimal to Binary, Binary to Decimal: the Doubling Method
Two directions, two algorithms, both sub-ten-second once drilled. Decimal to binary uses division by two with remainders: divide 173 by 2 to get 86 remainder 1, 86 by 2 is 43 remainder 0, 43 by 2 is 21 remainder 1, 21 by 2 is 10 remainder 1, 10 by 2 is 5 remainder 0, 5 by 2 is 2 remainder 1, 2 by 2 is 1 remainder 0, 1 by 2 is 0 remainder 1. Read the remainders bottom to top and 173 is 10101101. Faster for small numbers: subtract the largest power of two that fits, mark a 1 in that column, continue with the remainder. For 173, 128 fits leaving 45, 32 fits leaving 13, 8 fits leaving 5, 4 fits leaving 1, 1 fits leaving 0. Columns 128, 32, 8, 4, 1 give binary 10101101 again, matching.
Binary to decimal reverses the process: sum the power-of-two values where you see a 1. For 11011010, columns 128 + 64 + 16 + 8 + 2 = 218. The doubling variant runs left to right: start with 0, for each bit double the running total and add the bit. The bits 1, 1, 0, 1, 1, 0, 1, 0 produce 1, 3, 6, 13, 27, 54, 109, 218 in eight steps. Doubling avoids tracking column weights and scales to any bit width without pausing to recall 2^k. Our daily drill set rotates binary, hex, and decimal conversions at a length you choose, so the method becomes automatic within two weeks.
Hex Shortcuts: Why Four Bits Beats Three
Hex exists because 16 is 2^4, which makes each hex digit a clean four-bit chunk with zero arithmetic required. Memorize the sixteen values 0 through 9 plus A = 10, B = 11, C = 12, D = 13, E = 14, F = 15, and 0x7F becomes 7 and F in two glances: 7 is 0111, F is 1111, concatenate to 01111111, which is 127. Reverse works the same way. The bit pattern 11010110 splits into 1101 and 0110, hex D6, decimal 214. Programmers who touch RGB colors, memory addresses, or network packets save one step per conversion by never leaving the hex-to-bit mapping. The hex color #FF8800 decomposes to red 255, green 136, blue 0 in under three seconds once the digit table is automatic.
Hex to decimal without passing through binary uses place values 1, 16, 256, 4096. For 0x3A7 that is 3 256 + 10 16 + 7 = 768 + 160 + 7 = 935. The multiply-by-16 step is a left shift of four bits, which the next section turns into a universal reflex. When the inner multiplies get awkward, lean on the two-digit multiplication method and the hex conversion stops being the slow step.
Shift Arithmetic: Multiplying and Dividing by Powers of Two
A left shift by k multiplies by 2^k, a right shift by k divides by 2^k and discards the remainder. This is not an optimization curiosity, it is how your mental model should handle any power-of-two factor. To compute 47 * 32, recognise 32 as 2^5 and double 47 five times: 47, 94, 188, 376, 752, 1504. Five doublings takes under four seconds. To divide 10,000 by 128, note 128 is 2^7 and halve seven times: 10000, 5000, 2500, 1250, 625, 312, 156, 78 with integer truncation. Signed negative numbers behave the same under left shift until the sign bit flips, which caps safe shifts at 30 on a 32-bit int.
Mixed factors factor the power of two out first. For 48 25, write 48 as 16 3, giving 16 3 25 = 16 * 75 = 1200. Four doublings of 75 run 75, 150, 300, 600, 1200. The same split works in reverse: 384 / 12 becomes 384 / 4 / 3 = 96 / 3 = 32. Shift-first discipline removes the awkward middle multiply from most programmer arithmetic and shows up again in bitmask work, hash bucket sizing, and page-aligned memory offsets where alignment is a right shift followed by a left shift.
Estimating Big-O and Memory in Your Head
Algorithm analysis demands a one-shot estimate: for n = 1,000,000, what is the feasibility ceiling at O(n), O(n log n), and O(n^2). Treat log2(n) as the position of the highest bit, which for a million is 20 (since 2^20 is roughly 10^6). An O(n log n) sort on a million rows runs about 20 million operations, well under a second on modern hardware. O(n^2) at the same size runs 10^12 operations, which on a billion-ops-per-second CPU is a thousand seconds of pain, call it 17 minutes. O(2^n) doubles with each input, so n = 30 is a billion, n = 40 is a trillion, n = 50 is a quadrillion. Match the algorithm to the input by asking two questions: what is log2(n), and does n or n^2 exceed 10^8.
Memory sizing uses the same machinery. One million 8-byte doubles takes 8 MB, one billion takes 8 GB, which crosses the typical laptop RAM ceiling. A hash table at load factor 0.75 on 10 million entries needs around 13 million slots, call it 2^24, which at 16 bytes per slot is 256 MB. Capacity planning that used to need a spreadsheet collapses to a chain of doublings and the anchor values from section one. The mental math drill plan runs a daily twenty-question set that keeps these reflexes sharp without burning more than six minutes a day.


