On this page you can compute the magic number for division by a given divisor, and the multiplicative inverse modulo 2**32, of any number in the range –2**31 to 2**32 – 1.

Magic numbers are used to convert division by a constant into a short program that uses the most significant 32 bits of the 64-bit product of the dividend and the magic number. Multiplicative inverses are used to convert "exact division" by a constant into an ordinary multiplication modulo 2**32.

Examples of magic numbers are shown on page 189 of Hacker's Delight, and examples of multiplicative inverses are shown on page 197.

If you enter a divisor greater than or equal to 2**31, then for signed division it will be interpreted as a negative number (think of its interpretation as a 32-bit signed integer). Similarly, if you enter a negative number, then for UNsigned division it will be interpreted as a large positive number.

Enter a divisor:

Magic number for signed division: Shift amount:
Magic num. for unsigned division: Shift amount: "Add" indicator:
Multiplicative inverse:

Home