|
| 1 | +--- |
| 2 | +Title: '.BigMul()' |
| 3 | +Description: 'Multiplies two integer values and returns the full extended result to prevent overflow.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Code Foundations' |
| 7 | +Tags: |
| 8 | + - 'Methods' |
| 9 | + - 'Numbers' |
| 10 | + - 'Arithmetic' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-c-sharp' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +Math.BigMul(x, y); |
| 22 | +``` |
| 23 | + |
| 24 | +**Parameters:** |
| 25 | + |
| 26 | +- `x`, `y`: The integers to be multiplied. These can be of type `int`, `uint`, `long`, or `ulong`. |
| 27 | + |
| 28 | +**Return value:** |
| 29 | + |
| 30 | +Returns the full product of `x` and `y` as a single 64-bit or 128-bit integer, depending on the input type: |
| 31 | + |
| 32 | +- If `x` and `y` are of type `int` or `uint`, the return type is `long` and `ulong`, respectively. |
| 33 | +- If `x` and `y` are of type `long` or `ulong`, the return type is `Int128` and `UInt128`, respectively. |
| 34 | + |
| 35 | +> **Note:** The `Int128` and `UInt128` types 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. |
| 36 | +
|
| 37 | +## Example: Basic Usage |
| 38 | + |
| 39 | +In this example, the full product of two `int` values is calculated without overflow: |
| 40 | + |
| 41 | +```cs |
| 42 | +using System; |
| 43 | + |
| 44 | +public class Example { |
| 45 | + public static void Main() { |
| 46 | + // Integer values (32-bit) |
| 47 | + int intX = 100000; |
| 48 | + int intY = 500000; |
| 49 | + |
| 50 | + // Compute the product safely |
| 51 | + long intResult = Math.BigMul(intX, intY); |
| 52 | + |
| 53 | + Console.WriteLine($"Math.BigMul({intX}, {intY}) = {intResult}"); |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +This example outputs the following: |
| 59 | + |
| 60 | +```shell |
| 61 | +Math.BigMul(100000, 500000) = 50000000000 |
| 62 | +``` |
| 63 | + |
| 64 | +## Codebyte Example |
| 65 | + |
| 66 | +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: |
| 67 | + |
| 68 | +```codebyte/csharp |
| 69 | +using System; |
| 70 | +
|
| 71 | +public class Example { |
| 72 | + public static void Main (string[] args) { |
| 73 | + int x = 200000; |
| 74 | + int y = 300000; |
| 75 | +
|
| 76 | + // Normal multiplication (overflow) |
| 77 | + int normalResult = x * y; |
| 78 | +
|
| 79 | + // Math.BigMul() (no overflow) |
| 80 | + long bigMulResult = Math.BigMul(x, y); |
| 81 | +
|
| 82 | + Console.WriteLine($"Normal multiplication (overflow): {normalResult}"); |
| 83 | + Console.WriteLine($"BigMul (no overflow): {bigMulResult}"); |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
0 commit comments