Number Base & Modular Arithmetic Drills

Practice base conversion, modulo, modular inverses and two’s complement — including the cases where the % operator gives a different answer.

The traps this drills

  • The % operator is not modulo. In JavaScript, C, Java and most of their relatives it is REMAINDER, which keeps the sign of the number you started with — so -7 % 3 gives -1, while -7 mod 3 is 2. Python’s % gives the mathematical answer. Same symbol, different operation depending on where you type it, which is why this catches people who have been programming for years.
  • That difference is the source of a lot of quiet bugs: clock arithmetic that goes negative, an array index that wraps the wrong way, a hash landing outside its table. The fix in most languages is ((a % n) + n) % n, which is what this tool uses everywhere it means modulo.
  • A modular inverse usually does not exist. There is a number x with a times x leaving 1 modulo n only when a and n share no factor — so 6 has no inverse modulo 9, and the honest answer is "none" rather than a number that nearly works. The drill asks questions with no answer on purpose, because recognising that is the actual skill.
  • "Minus five in binary" is not a complete question. Two’s complement needs a width: minus five is 1011 in four bits and 11111011 in eight, and neither is more correct. Any tool that answers without asking has chosen a width for you and not mentioned it.
  • Every question here is checked against an independent calculation before it is asked — the answer is worked out one way and verified another. A drill that marks a right answer wrong is worse than no drill, so that check runs on thousands of generated questions before shipping.

Or work one out

The drill asks the questions. This answers them, for your own numbers — including the two that the drill exists to separate, and the case where there is no answer at all.

How to use

  1. Pick which topics you want to practise.
  2. Answer, then read why — including the trap where one applies.
  3. Keep a streak going; the global board ranks the longest.

Frequently asked questions

Why did I get a negative modulo question wrong?

Probably because you gave what the % operator would. In JavaScript, C and Java, % is REMAINDER and keeps the sign of the number you started with, so -7 % 3 is -1. Modulo in mathematics always lands between 0 and n-1, so -7 mod 3 is 2. Python’s % gives the mathematical answer, which is exactly why the confusion survives.

How do I get the mathematical answer in JavaScript?

Write ((a % n) + n) % n. Taking the remainder once can leave you negative; adding n and taking it again brings it into range without changing which class it belongs to. That is what this tool uses everywhere it means modulo.

Why does it ask questions with no answer?

Because recognising them is the actual skill. A modular inverse exists only when the two numbers share no factor, so 6 has no inverse modulo 9 — and the right answer is "none", not a number that nearly works. Over a run of 4,000 generated inverse questions, 1,147 genuinely had no answer.

Why does two’s complement ask for a width?

Because "minus five in binary" is not a complete question. Minus five is 1011 in four bits and 11111011 in eight, and neither is more correct than the other. Any tool that answers without asking has picked a width for you and not mentioned it.

Could it mark a right answer wrong?

It is built hard against that. Every generated question is verified by reading the question back and working the answer out a second, independent way — 20,000 questions across all five kinds were put through that before shipping. Marking also forgives spacing, capital letters and leading zeros, since a drill that tests your typing is testing the wrong thing.

What is the global board ranking?

The longest unbroken run of correct answers. Which topics you selected is not recorded, so a run of base conversions and a run of modular inverses sit on the same table — treat it as a bit of fun rather than a like-for-like ranking.

🔒 This tool runs entirely in your browser. Nothing you enter is uploaded, logged, or stored.