Facebook Page Twitter Page LinkedIn Page
× C# Tutorial C# Advanced C# Resources


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

  • If the argument is NaN, PositiveInfinity or NegativeInfinity, 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 below example, 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(2)); 
    Console.WriteLine(Math.ILogB(10)); 
    Console.WriteLine(Math.ILogB(32));  
  }
}

The output of the above code will be:

1
3
5

❮ C# Math Methods

5