C# Tutorial C# Advanced C# References

C# Math - ILogB() Method



The C# ILogB() method returns the base-2 integer logarithm of a given number. In special cases it returns the following:

  • If the argument is NaN, positive infinity or negative infinity, the method returns Int32.MaxValue which is 2147483647.
  • If the argument is zero, the method returns Int32.MinValue which is -2147483648.

Syntax

public static int ILogB (double arg);

Parameters

arg Specify the number whose logarithm is to be found.

Return Value

Returns the base-2 integer logarithm of a given number.

Example:

In the example below, ILogB() method is used to calculate the base-2 integer logarithm of a given number.

using System;

class MyProgram {
  static void Main(string[] args) {
    Console.WriteLine("Math.ILogB(0) = "
                      + Math.ILogB(0));
    Console.WriteLine("Math.ILogB(10) = "
                      + Math.ILogB(10));
    Console.WriteLine("Math.ILogB(50) = "
                      + Math.ILogB(50));
    Console.WriteLine("Math.ILogB(Double.NaN) = "
                      + Math.ILogB(Double.NaN));
    Console.WriteLine("Math.ILogB(Double.NegativeInfinity) = "
                      + Math.ILogB(Double.NegativeInfinity));
    Console.WriteLine("Math.ILogB(Double.PositiveInfinity) = "
                      + Math.ILogB(Double.PositiveInfinity));
  }
}

The output of the above code will be:

Math.ILogB(0) = -2147483648
Math.ILogB(10) = 3
Math.ILogB(50) = 5
Math.ILogB(Double.NaN) = 2147483647
Math.ILogB(Double.NegativeInfinity) = 2147483647
Math.ILogB(Double.PositiveInfinity) = 2147483647

❮ C# Math Methods

5