C# Math ILogB() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.ILogB(x) returns the binary exponent of a number as an int. In plain words, it finds the biggest power of two that is not larger than the size of x and returns that power. Math.ILogB(10) is 3, because 23 = 8 fits into 10 but 24 = 16 does not.

Think of a ladder with rungs at 1, 2, 4, 8, 16, 32 and so on. ILogB tells you which rung you have passed. It is like (int)Math.Floor(Math.Log2(Math.Abs(x))), but it reads the exponent straight from the stored bits, so it is exact and fast. It is an advanced tool, and the partner of ScaleB(). The "I" stands for "integer".

This is a static member of the static class System.Math. You call it with the class name, such as Math.ILogB(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Math.ILogB was added in .NET Core 3.0. It is not in classic .NET Framework.

Syntax

Pass a double. The answer is an int.

C#
int a = Math.ILogB(10);      // 3  (8 <= 10 < 16)
int b = Math.ILogB(1);       // 0
int c = Math.ILogB(0.5);     // -1
CallWhat it does
Math.ILogB(8)Returns 3.
Math.ILogB(10)Also 3. 10 is between 8 and 16.
Math.ILogB(0.1)Returns -4. 0.1 is between 1/16 and 1/8.
Math.ILogB(0)Returns int.MinValue. Zero has no exponent.

The sign of x is ignored: ILogB(-16) is 4, the same as ILogB(16). Zero returns int.MinValue and infinity returns int.MaxValue, as markers rather than real exponents. Do not rely on the exact value for NaN; test it with double.IsNaN before calling.

Library declarations
C#
public static int ILogB(double x);

Parameters

ParameterDescription
xRequired. The number whose binary exponent you want. The sign is ignored.

Return Value

Return ValueDescription
intThe unbiased binary exponent of x: the whole number e such that 2e <= |x| < 2e+1. int.MinValue for 0, int.MaxValue for infinity.

C# Examples

Example 1 — Which Power of Two Fits?

Call ILogB on a range of numbers, including a fraction.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.ILogB(1));
        Console.WriteLine(Math.ILogB(2));
        Console.WriteLine(Math.ILogB(8));
        Console.WriteLine(Math.ILogB(10));
        Console.WriteLine(Math.ILogB(1000));
        Console.WriteLine(Math.ILogB(0.5));
        Console.WriteLine(Math.ILogB(0.1));
    }
}

How It Works

  1. 1 and 2. 1 is 2 to the power 0, so the answer is 0. 2 is 2 to the power 1, so the answer is 1.
  2. 8 and 10. 8 is exactly 23, so 3. 10 is bigger than 8 but smaller than 16, so it also gets 3.
  3. 1000. The powers around it are 512 (29) and 1024 (210). Since 512 is the one that fits, the answer is 9.
  4. Numbers below 1. 0.5 is 2-1, so -1. 0.1 lies between 1/16 (2-4) and 1/8, so the answer is -4.

Example 2 — Split a Number with ILogB and ScaleB

Compare ILogB with Log2, then divide out the exponent and put it back.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        double x = 40;
        Console.WriteLine(Math.ILogB(x));
        Console.WriteLine(Math.Floor(Math.Log2(x)));

        double mantissa = Math.ScaleB(x, -Math.ILogB(x));
        Console.WriteLine(mantissa.ToString(CultureInfo.InvariantCulture));
        Console.WriteLine(Math.ScaleB(mantissa, Math.ILogB(x)));
    }
}

How It Works

  1. The exponent. 40 lies between 32 (25) and 64 (26), so Math.ILogB(40) is 5.
  2. Same as floor of Log2. Math.Log2(40) is about 5.32, and its floor is 5. ILogB gives that whole number directly as an int.
  3. Divide out the power. ScaleB(40, -5) divides by 32 and leaves 1.25. Every number can be written as something from 1 up to (but not including) 2, times a power of two.
  4. Put it back. ScaleB(1.25, 5) multiplies by 32 and returns exactly 40. No rounding error appears.

Example 3 — Zero, Negative, Infinity, and Tiny Numbers

See the marker values for zero and infinity, the sign being ignored, and the smallest positive double.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.ILogB(-16));
        Console.WriteLine(Math.ILogB(0) == int.MinValue);
        Console.WriteLine(Math.ILogB(double.PositiveInfinity) == int.MaxValue);
        Console.WriteLine(Math.ILogB(double.Epsilon));
    }
}

How It Works

  1. Sign ignored. -16 has the same size as 16, which is 24. The answer is 4, not an error.
  2. Zero. Zero is not a power of two times anything, so there is no real exponent. ILogB returns the marker int.MinValue, and the comparison prints True.
  3. Infinity. Infinity also has no real exponent. The marker is int.MaxValue, so this comparison is True too.
  4. The tiniest double. double.Epsilon is the smallest positive double, 2 to the power -1074. ILogB reports -1074.

Frequently Asked Questions

It returns the binary exponent of a number as an int: the power of two just at or below the size of the number. Math.ILogB(10) is 3 because 8 (2 to the power 3) fits in 10 but 16 does not.
Log2 returns a double with decimals, such as 3.32 for 10. ILogB returns the whole exponent as an int, which is the floor of Log2 of the absolute value (3 for 10). ILogB also works on the stored bits, so it is exact.
It returns int.MinValue, a marker value meaning "no exponent". Infinity returns int.MaxValue. Check for these cases before using the result.
It was added in .NET Core 3.0, so it is in modern .NET. It is not in classic .NET Framework.
No. Math is a static class, so you call Math.ILogB(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: Log()

Return the natural logarithm of a number, or the logarithm in any base you choose.

Log() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful