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
Call
What 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
Parameter
Description
x
Required. A float, double, or long double. The sign is ignored. An integer is converted to double.
Return Value
Return Value
Description
Exponent
The binary exponent of x as an int.
FP_ILOGB0
Returned for 0.0. The exact value depends on the compiler.
FP_ILOGBNAN / INT_MAX
NaN 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;
}
FP_ILOGB0 and FP_ILOGBNAN are macros from <cmath>. The program never prints their numbers, because the values depend on the compiler.
std::nan("") creates a NaN, and ilogb() returns FP_ILOGBNAN for it, so the comparison is true.
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.