MySQL LOG2() Function: Calculate base-2 logarithms

LOG2(x) returns the binary logarithm of a positive number, useful for measuring powers of two.

LOG2(): Quick SQL Example

Run this SELECT query directly in MySQL or phpMyAdmin. It uses fixed inputs so you can verify the result without creating a table.

SELECT LOG2(16) AS log_value;
log_value
4
On this page — Show page sectionsHide page sections

More SQL Examples and Explanations

LOG2(X) returns the base-2 logarithm of X. It is useful when reasoning about powers of two and bit sizes.

SELECT LOG2(7); -- Output: 2.8073549220576
SELECT LOG2(0.1); -- Output: -3.32192809488736
SELECT LOG2(0); -- Output: NULL

Using ROUND() with LOG2()

SELECT ROUND(LOG2(8), 2); -- Output: 3.00
SELECT ROUND(LOG2(-5), 2); -- Output: NULL
SELECT ROUND(LOG2(9), 2); -- Output: 3.17

Syntax

SELECT LOG2(X);

How to Interpret the Result

LOG2() is only defined for positive inputs. Use LOG10() for base 10 or LOG() for the natural base.

Common Question

When is LOG2() useful?
It is useful when reasoning about powers of two, binary sizes, and doubling relationships.

Related: CONV()

↑ Back to top




Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer