Math for Programmers

Binary and hex, bitwise flags, boolean logic, sets, modulo, Big-O, counting, probability, and geometry — the everyday math hiding inside your code, learned by building a small toolkit.

Most programmers will tell you they don't use much math at work, and then spend the afternoon debugging a hex color, a permission check, a carousel that wraps around wrongly, a price that is off by one cent, or a loop that is fine with 100 users and falls over at 100,000. All of those are math problems. They just don't look like the math from school, so nobody warned you.

This series teaches the math that actually shows up in everyday programming, and it teaches it with code instead of proofs. You will see why 0.1 + 0.2 !== 0.3, how a single number can store a dozen yes/no permissions, how to simplify a tangled if using De Morgan's laws, why % makes a carousel wrap around, how many passwords exist in a given format, and what "O(n²)" really tells you about the code in front of you.

The first half covers how computers represent and combine values: number systems (binary, hex), bitwise operations and flags, boolean logic, sets, modular arithmetic, and the limits of integers and floating-point numbers. The second half covers how code scales and how to reason about chance: powers and logarithms, Big-O notation, counting (permutations and combinations), probability, and finally coordinates and geometry for interfaces and animation.

You won't just read about it. Across the series you build mathlab, a small JavaScript toolkit that runs in Node.js. Each chapter adds one module: a color converter, a permission-flag checker, a set helper, a carousel index, a money formatter, a benchmark runner, a password-space calculator, a collision simulator, and a few geometry helpers. By the end you have a tested library you wrote yourself, and you know where each piece of math came from.

You need to be comfortable with the basics of JavaScript — variables, functions, arrays, loops, and import/export — from JavaScript & TypeScript Fundamentals, and with the terminal from Command Line Basics. Bits and Bytes in How Computers Work is a helpful warm-up for chapters 2 and 3. You need Node.js (the LTS version) and a code editor such as Visual Studio Code. No math beyond school arithmetic is assumed, and nothing costs money.

Vectors, matrices, and calculus are deliberately left out. They are covered in the planned Math for AI series, where they are needed.

  1. 01Why Programmers Need MathWhich math shows up in everyday code and which does not, five small bugs that are really math bugs, and how to set up the Node.js folder called mathlab that we build in every chapter.
  2. 02Numbers and Number SystemsPositional notation, binary, octal, and hex by hand and with toString and parseInt, hex colors, and why 0.1 + 0.2 is not 0.3 — used to build a color converter and nearlyEqual.
  3. 03Bitwise Operations and FlagsAND, OR, XOR, NOT, and shifts, bitmasks, Unix permissions like chmod 755, feature flags, and JavaScript's 32-bit limit — used to build a permission-flag checker.
  4. 04Boolean LogicTruth tables, AND, OR, NOT, XOR, De Morgan's laws, short-circuit evaluation, and early returns — used to untangle a messy permission check and build a logic helper for mathlab.
  5. 05Sets and Set OperationsSets versus arrays, JavaScript's Set, union, intersection, difference, and subsets — used to answer questions like "who signed up but did not pay?" and to build a set helper for mathlab.
  6. 06Modular ArithmeticRemainder versus true modulo, clock math, even and odd, wraparound carousels, circular buffers, hash buckets, and the Luhn check digit — used to build a safe mod and a card-number validator for mathlab.
  7. 07Integers, Overflow and PrecisionWhy 0.1 + 0.2 is not 0.3, what happens when a number gets too big for its box, how BigInt helps, and why money should be stored as whole cents — building the mathlab money module.
  8. 08Functions, Growth and LogarithmsFunctions as input-to-output machines, how linear, quadratic and exponential growth pull apart, why logarithms mean 'how many times can I halve', and how binary search finds one item in a million in about 20 steps.
  9. 09Big-O NotationA plain-language guide to Big-O: how to read O(1), O(log n), O(n), O(n log n), O(n^2) and O(2^n) from code, how to measure with performance.now, and how Set.has and binary search beat a linear scan.
  10. 10Counting: Permutations and CombinationsCount possibilities without listing them — the rule of product, factorials, permutations, combinations, password and PIN spaces, and pagination — then add a counting module to mathlab that uses BigInt for huge answers.
  11. 11Probability BasicsMeasure chance as favorable over total, combine independent events, compute expected value, simulate with Math.random, understand the birthday paradox and ID collisions, and add a probability module to mathlab.
  12. 12Coordinates, Geometry and Putting It TogetherPlace points on a screen, measure distance with Pythagoras, move around a circle with sin and cos, animate with lerp and clamp, then assemble every mathlab module into one report and see where each idea shows up in real work.