C# .BigMul()
The Math.BigMul() method multiplies two integer values and returns the full extended result to avoid overflow. It ensures that the product fits within a larger data type when the operands are 32-bit or 64-bit integers.
Syntax
Math.BigMul(x, y);
Parameters:
x,y: The integers to be multiplied. These can be of typeint,uint,long, orulong.
Return value:
Returns the full product of x and y as a single 64-bit or 128-bit integer, depending on the input type:
- If
xandyare of typeintoruint, the return type islongandulong, respectively. - If
xandyare of typelongorulong, the return type isInt128andUInt128, respectively.
Note: The
Int128andUInt128types were introduced in .NET 7. In earlier versions,Math.BigMul(long, long, out long high)can be used to obtain the high and low 64-bit parts of the product.
Example: Basic Usage
In this example, the full product of two int values is calculated without overflow:
using System;public class Example {public static void Main() {// Integer values (32-bit)int intX = 100000;int intY = 500000;// Compute the product safelylong intResult = Math.BigMul(intX, intY);Console.WriteLine($"Math.BigMul({intX}, {intY}) = {intResult}");}}
This example outputs the following:
Math.BigMul(100000, 500000) = 50000000000
Codebyte Example
The following example demonstrates the use of Math.BigMul() to calculate the product of two integers that would normally cause an overflow with standard multiplication:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
- Beginner Friendly.15 hours