log2

log2(x)

Try it yourself:

See also:

log2(): Base-2 Logarithm

log2() returns the exponent of 2 that produces a positive input. It is useful for binary data sizes, algorithmic complexity, tree depth, and repeated halving.

Binary interpretation

log2(8) is 3 because 2³ is 8. Real inputs must be positive; zero and negative values have no real logarithm. Reverse a result using pow(), and use log() when a natural-log formula is required.

In data structures, a base-2 logarithm often describes how many balanced decisions are needed to search a range. Use floor() or ceil() only after deciding whether partial levels should count.

Try Log2 in Calcul.io

Start with one of the editable examples above, then replace its arguments with your own values. Keeping the function on a separate calculator line makes the input and result easy to compare. For a longer workflow, assign the result to a variable or reference that line in the next expression.

Check the shown signature before adding optional arguments, and use the related-function links to compare operations with similar purposes.

All functions