C# Math BigMul() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.BigMul(a, b) multiplies two integers and keeps the full, exact answer. With two int values you get a long, which is big enough to hold the result of any int times any int.

Imagine a calculator display with only 10 digits. If you multiply two 6-digit numbers, the answer can be 12 digits long and the extra digits are lost. In C#, an int times an int is also an int, so a big product silently wraps around to a wrong number. BigMul avoids this. For two 64-bit numbers, the full answer needs 128 bits, so the method returns the high half and gives you the low half through an out variable. That second form is an advanced tool, used in hashing and big-number code.

This is a static member of the static class System.Math. You call it with the class name, such as Math.BigMul(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. The int version, Math.BigMul(int, int), returns a long. The 64-bit versions with an out parameter need .NET 5 or later.

Syntax

With two int values, pass both and read a long. With two 64-bit values, pass both plus an out variable for the low half.

C#
long product = Math.BigMul(100000, 100000);     // 10000000000

// 64-bit numbers (.NET 5 and later): high half returned, low half in out
ulong high = Math.BigMul(ulong.MaxValue, 2UL, out ulong low);   // high = 1
CallWhat it does
Math.BigMul(100000, 100000)Returns the long 10000000000. Plain 100000 * 100000 would give a wrong int.
Math.BigMul(int.MaxValue, int.MaxValue)Returns 4611686014132420609, the largest possible product of two int values, which still fits in a long.
Math.BigMul(a, b, out ulong low)For ulong inputs. Returns the high 64 bits and sets low to the low 64 bits.
checked(a * b)The alternative that throws OverflowException instead of giving a bigger result.

The int version never overflows. The 64-bit versions return the product as two 64-bit halves: the real answer is high * 264 + low. For the signed long version, low is a long holding the raw low bits, so use the ulong version if you want easy-to-read numbers.

Library declarations
C#
public static long BigMul(int a, int b);
public static long BigMul(long a, long b, out long low);        // .NET 5+
public static ulong BigMul(ulong a, ulong b, out ulong low);    // .NET 5+

Parameters

ParameterDescription
aRequired. The first number to multiply.
bRequired. The second number to multiply.
lowOnly in the 64-bit versions. An out variable that receives the low 64 bits of the 128-bit product.

Return Value

Return ValueDescription
long (for int inputs)The exact product of the two int values.
long / ulong (for 64-bit inputs)The high 64 bits of the 128-bit product. The low 64 bits are returned through low.

C# Examples

Example 1 — A Product That Does Not Fit in an int

Multiply 100000 by 100000 three ways: wrapped around, exact with BigMul, and checked.

C#
using System;

class Program {
    static void Main() {
        int a = 100000;
        int b = 100000;

        int wrong = unchecked(a * b);
        long right = Math.BigMul(a, b);
        Console.WriteLine(wrong);
        Console.WriteLine(right);

        try {
            int safe = checked(a * b);
            Console.WriteLine(safe);
        } catch (OverflowException) {
            Console.WriteLine("overflow");
        }
    }
}

How It Works

  1. The silent mistake. The true answer is 10 billion, but an int holds only up to about 2.1 billion. The extra bits are thrown away and you get the wrong number, 1410065408.
  2. The exact answer. Math.BigMul(a, b) returns a long with the full product, 10000000000.
  3. checked mode. Wrapping the multiplication in checked(...) tells C# to stop on overflow rather than wrap around.
  4. The exception. The product does not fit, so OverflowException is thrown and the catch block prints a word. BigMul gives you the answer, checked gives you an alarm.

Example 2 — Biggest and Negative Products

Try the largest int values, a product just past int.MaxValue, and a negative product.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.BigMul(int.MaxValue, int.MaxValue));
        Console.WriteLine(Math.BigMul(46341, 46341));
        Console.WriteLine(Math.BigMul(-50000, 70000));
        Console.WriteLine(Math.BigMul(46341, 46341) > int.MaxValue);
    }
}

How It Works

  1. The biggest product. int.MaxValue times itself is 4611686014132420609. It is huge, but a long can hold it, so there is no overflow.
  2. Just past the limit. 46341 x 46341 is 2147488281, a little more than int.MaxValue (2147483647). This is the smallest square that overflows an int.
  3. Negative product. -50000 x 70000 is -3500000000. The sign works as usual.
  4. The test. Because the result is a long, comparing it with int.MaxValue is safe and prints True. You can use this to detect overflow before storing the result.

Example 3 — High and Low Halves of a 128-bit Product

Multiply two ulong values and read both halves of the answer (needs .NET 5 or later).

C#
using System;

class Program {
    static void Main() {
        ulong high = Math.BigMul(ulong.MaxValue, 2UL, out ulong low);
        Console.WriteLine(high);
        Console.WriteLine(low);

        ulong high2 = Math.BigMul(1UL << 40, 1UL << 40, out ulong low2);
        Console.WriteLine(high2);
        Console.WriteLine(low2);
    }
}

How It Works

  1. Two halves. A product of two 64-bit numbers can need 128 bits. The method returns the top 64 bits and puts the bottom 64 bits into low.
  2. Max times 2. ulong.MaxValue is 264 - 1. Doubling it gives 265 - 2, which is 1 lot of 264 plus 18446744073709551614.
  3. Powers of two. 240 times 240 is 280. That equals 65536 lots of 264, so high2 is 65536.
  4. Nothing left over. 280 is an exact multiple of 264, so the low half is 0. Real code combines the halves as high * 264 + low.

Frequently Asked Questions

It multiplies two integers and keeps the full answer. With two int values it returns a long, so there is no overflow. Math.BigMul(100000, 100000) is 10000000000.
For two int values, a * b is also an int, so a big product wraps around to a wrong number without any warning. BigMul returns a long that can hold any int times int.
BigMul gives you the correct bigger answer. checked(a * b) throws OverflowException when the answer does not fit. Pick BigMul when you want the number, and checked when overflow means a bug.
The versions that take long or ulong values and return the high half with an out low half need .NET 5 or later. The int version returning a long is older.
No. Math is a static class, so you call Math.BigMul(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: BitDecrement()

Return the very next double below a number: the smallest step down that a double can show.

BitDecrement() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful