C++ std::ilogb() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::ilogb(x) gives the binary exponent of a number as an int. It is the power of two that sits just below |x|. For 10 the answer is 3, because 23=8 is the biggest power of two that is not larger than 10.

It is the integer twin of std::logb(). Use it when you want to do whole-number math with the exponent, for example to shift bits. The special inputs 0, infinity, and NaN return special constants from <cmath>.

std::ilogb() is declared in <cmath> and is part of the group Exponential and logarithm. Exponentials and logarithms undo each other. A logarithm of zero is -inf, and a logarithm of a negative number is NaN.

Syntax

Include <cmath> and call std::ilogb. Start with these lines.

C++
int a = std::ilogb(8.0);     // 3
int b = std::ilogb(10.0);    // 3
int c = std::ilogb(0.1);     // -4
CallWhat it does
std::ilogb(8.0)8 is exactly 23. Returns 3.
std::ilogb(0.1)0.1 is below one, so the exponent is negative. Returns -4.
std::ilogb(0.0)Zero has no exponent. Returns FP_ILOGB0.

The result is an int. Unlike logb(), you can use it directly as a shift count or an array index.

Library declarations
C++
int ilogb(float x);
int ilogb(double x);
int ilogb(long double x);
int ilogb(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. A float, double, or long double. The sign is ignored. An integer is converted to double.

Return Value

Return ValueDescription
ExponentThe binary exponent of x as an int.
FP_ILOGB0Returned for 0.0. The exact value depends on the compiler.
FP_ILOGBNAN / INT_MAXNaN gives FP_ILOGBNAN, and infinity gives INT_MAX.

C++ Examples

Example 1 — Binary Exponents of Several Numbers

Print the int exponent of five numbers, from small to large.

C++
#include <cmath>
#include <iostream>

int main() {
    double values[] = {8.0, 10.0, 0.1, 1000.0, 0.5};

    for (double v : values) {
        std::cout << "ilogb(" << v << ") = " << std::ilogb(v) << '\n';
    }
    return 0;
}

How It Works

  1. The loop gives each number to std::ilogb() and prints the int that comes back.
  2. 8 and 10 both give 3, because both are at least 23 and less than 24.
  3. Numbers below 1 have negative exponents: 0.5 is 2-1 and 0.1 gives -4. 1000 gives 9.

Example 2 — ilogb() Next to logb()

Both functions give the same exponent. The type is what changes, and the int can be used in a bit shift.

C++
#include <cmath>
#include <iostream>

int main() {
    double x = 40.0;
    int e = std::ilogb(x);
    double d = std::logb(x);

    std::cout << "ilogb(40) = " << e << '\n';
    std::cout << "logb(40)  = " << d << '\n';
    std::cout << "2^e as int = " << (1 << e) << '\n';
    std::cout << "ilogb(0.3) = " << std::ilogb(0.3) << '\n';
    return 0;
}

How It Works

  1. 40 lies between 32 (25) and 64 (26), so ilogb(40) is 5.
  2. logb(40) also prints 5. The difference is that it was stored in a double and this one in an int.
  3. Because e is an int, 1 << e works and gives 32. For 0.3 the exponent is -2, because 0.3 is 1.2 * 2-2.

Example 3 — Special Values: Zero, Infinity, NaN

Compare the result with the special macros before you trust it.

C++
#include <cmath>
#include <iostream>
#include <climits>
#include <limits>

int main() {
    double inf = std::numeric_limits<double>::infinity();
    double bad = std::nan("");

    std::cout << std::boolalpha;
    std::cout << "ilogb(0)   == FP_ILOGB0:   " << (std::ilogb(0.0) == FP_ILOGB0) << '\n';
    std::cout << "ilogb(NaN) == FP_ILOGBNAN: " << (std::ilogb(bad) == FP_ILOGBNAN) << '\n';
    std::cout << "ilogb(inf) == INT_MAX:     " << (std::ilogb(inf) == INT_MAX) << '\n';
    return 0;
}

How It Works

  1. FP_ILOGB0 and FP_ILOGBNAN are macros from <cmath>. The program never prints their numbers, because the values depend on the compiler.
  2. std::nan("") creates a NaN, and ilogb() returns FP_ILOGBNAN for it, so the comparison is true.
  3. For infinity the function returns INT_MAX, which is why <climits> is included. std::boolalpha prints the words true.

Frequently Asked Questions

std::ilogb(x) returns the binary exponent of x as an int. It is the whole-number part of log2(abs(x)), so ilogb(10.0) is 3.
An int with the exponent of x. For 0 it returns FP_ILOGB0, for NaN it returns FP_ILOGBNAN, and for infinity it returns INT_MAX.
It returns FP_ILOGB0, a macro from <cmath>. Its value depends on the compiler and is often INT_MIN. Compare with the macro and do not print it as a real exponent.
Use ilogb() when you want an int, for example to shift bits or pick an array index. Use logb() when you want a double and the value -inf for zero.
Include <cmath> and write std::ilogb(). That always works and picks the C++ overloads. The global name ::ilogb often works too, but the standard does not promise it, and it may only take a double.

Next: isfinite()

Keep going through the <cmath> functions. Checks that a number is not infinity and not NaN.

isfinite() 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.

8 people found this page helpful