Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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.
