C# Math.BigMul() – Examples

In this tutorial, we will learn about the C# Math.BigMul() method, and learn how to use this method to multiply big integers like Int32, Int64, etc., with the help of examples.

BigMul(Int32, Int32)

Math.BigMul(a, b) produces the full product of two 32-bit numbers a and b.

ADVERTISEMENT

Syntax

The syntax of BigMul(a, b) method is

Math.BigMul(Int32 a, Int32 b)

where

ParameterDescription
aThe first number of type Int32 to multiply.
bThe second number of type Int32 to multiply.

Return Value

The method returns value of type Int64.

Example 1 – BigMul(a, b)

In this example, we will multiply two numbers, a and b, of type Int32 using Math.BigMul() method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        Int32 a, b;
        Int64 result;

        a = 285689659;
        b = 799999992;
        result = Math.BigMul(a, b);
        Console.WriteLine($"BigMul({a},{b}) = {result}");

        a = -28599634;
        b = -8544192;
        result = Math.BigMul(a, b);
        Console.WriteLine($"BigMul({a},{b}) = {result}");
    }
}

Output

BigMul(285689659,799999992) = 228551724914482728
BigMul(-28599634,-8544192) = 244360764025728

BigMul(Int64, Int64, Int64)

Math.BigMul(a, b, low) produces the full product of two 64-bit numbers a and b.

Syntax

The syntax of BigMul(a, b, low) method is

Math.BigMul(Int64 a, Int64 b, out Int64 low)

where

ParameterDescription
aThe first number to multiply.
bThe second number to multiply.
lowThe low 64-bit of the product of the specified numbers.

Return Value

The method returns Int64 value. The higher 64 bits of the product is returned and the lower 64 bits of the product is stored in low.

Example 2 – BigMul(a, b, low)

In this example, we will take two very large numbers in Int64 variables, and find their product using Math.BigMul() method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        Int64 a, b;
        Int64 low, result;

        a = 2856896599999999919;
        b = 1856896599999987580;
        result = Math.BigMul(a, b, out low);
        Console.WriteLine($"result = {result}");
        Console.WriteLine($"low    = {low}");
    }
}

Output

result = 287582543666998578
low    = 3922365799111403972

BigMul(UInt64, UInt64, UInt64)

Math.BigMul(a, b, low) produces the full product of two unsigned 64-bit numbers a and b.

Syntax

The syntax of BigMul(UInt64, UInt64, UInt64) method is

Math.BigMul(UInt64 a, UInt64 b, out UInt64 low)

where

ParameterDescription
aThe first number to multiply.
bThe second number to multiply.
lowThe low 64-bit of the product of the specified numbers.

Return Value

The method returns high 64-bit of the product of the specified numbers, a value of type UInt64.

Example 3 – BigMul(a, b, low)

In this example, we will two unsigned long numbers and find their product using Math.BigMul() method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        UInt64 a, b;
        UInt64 low, result;

        a = 2856896599999999919;
        b = 1856896599999987580;
        result = Math.BigMul(a, b, out low);
        Console.WriteLine($"result = {result}");
        Console.WriteLine($"low    = {low}");
    }
}

Output

result = 287582543666998578
low    = 3922365799111403972

Conclusion

In this C# Tutorial, we have learnt the syntax of C# Math.BigMul() method, and also learnt how to use this method with the help of C# example programs.