Math.BigMul() Method in C#

The Math.BigMul() method in C# is used to calculate the full product of two 32-bit integers. This method is particularly useful when multiplying large integers that might produce a result exceeding the range of a 32-bit integer, as it returns a 64-bit long value to accommodate the full product.

Syntax

Following is the syntax −

public static long BigMul(int val1, int val2);

Parameters

  • val1: The first 32-bit integer to multiply

  • val2: The second 32-bit integer to multiply

Return Value

Returns a long (64-bit integer) containing the full product of the two input parameters.

Math.BigMul() Operation int val1 32-bit × int val2 32-bit = long result 64-bit Prevents overflow by using 64-bit result Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Using Math.BigMul() with Maximum Integer Values

When multiplying large integers like Int32.MaxValue, the result would overflow a standard int multiplication, but Math.BigMul() handles this safely −

using System;

public class Demo {
    public static void Main() {
        int val1 = Int32.MaxValue;
        int val2 = Int32.MaxValue;
        
        Console.WriteLine("val1 = " + val1);
        Console.WriteLine("val2 = " + val2);
        Console.WriteLine("Product using BigMul() = " + Math.BigMul(val1, val2));
        
        // Compare with regular multiplication (would overflow)
        Console.WriteLine("Regular multiplication = " + (long)val1 * val2);
    }
}

The output of the above code is −

val1 = 2147483647
val2 = 2147483647
Product using BigMul() = 4611686014132420609
Regular multiplication = 4611686014132420609

Using Math.BigMul() with Custom Values

using System;

public class Demo {
    public static void Main() {
        int val1 = 139897778;
        int val2 = 217878786;
        
        Console.WriteLine("First number: " + val1);
        Console.WriteLine("Second number: " + val2);
        Console.WriteLine("Product = " + Math.BigMul(val1, val2));
        
        // Demonstrate the difference with int overflow
        int regularProduct = val1 * val2;
        Console.WriteLine("Regular int multiplication (overflow): " + regularProduct);
    }
}

The output of the above code is −

First number: 139897778
Second number: 217878786
Product = 30480758034737508
Regular int multiplication (overflow): -1650967020

Practical Use Case

using System;

public class Demo {
    public static void Main() {
        // Calculate area of a large rectangle
        int length = 50000;
        int width = 75000;
        
        long area = Math.BigMul(length, width);
        Console.WriteLine($"Rectangle dimensions: {length} x {width}");
        Console.WriteLine($"Area using BigMul(): {area} square units");
        
        // Show what happens without BigMul
        int overflowArea = length * width;
        Console.WriteLine($"Area with regular multiplication: {overflowArea}");
    }
}

The output of the above code is −

Rectangle dimensions: 50000 x 75000
Area using BigMul(): 3750000000 square units
Area with regular multiplication: -544967296

Conclusion

The Math.BigMul() method prevents integer overflow when multiplying two 32-bit integers by returning a 64-bit long result. This method is essential when working with large integer calculations where the product might exceed the range of a standard int type.

Updated on: 2026-03-17T07:04:35+05:30

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements