Log2 calculator
Enter a positive number and get its base-2 logarithm, the power of two that produces it: log2 of 8 is 3, of one half is −1, and of 10 about 3.321928094887. Exact powers of two give exact whole answers. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Value
- 8
- Log2
- 3
- Expression
- log2(8) = 3
The base-2 logarithm of a positive number x is the unique real exponent y such that two to the power of y equals x. Computer science writes it as log2(x) or lg(x); ISO 80000-2 writes lb(x) so the binary log is not confused with log10. Students meet it when counting bits, solving 2^y = x, and converting between binary and decimal magnitudes.
What the base-2 logarithm is, and when log2(x) is defined
The base-2 logarithm of a positive real number x is the unique real number y that satisfies two to the power of y equals x. In symbols, y equals log2(x) if and only if 2^y equals x. The map is the inverse of the exponential with base two: log2 of two to the k is k for every real k in range, and two to the log2 of x recovers x for every positive x.
Several values are worth memorizing because they appear in every bit-counting argument. log2 of one is zero, because two to the power of zero is one. log2 of two is one. log2 of four is two. log2 of eight is three. log2 of sixteen is four, and in general log2 of two to the n is exactly n when n is an integer. Fractions below one produce negative logarithms: log2 of one half is minus one, and log2 of one fourth is minus two.
The function is strictly increasing on the positive reals, tends to minus infinity as x approaches zero from the right, and tends to plus infinity as x grows without bound. Use this page when you specifically need the binary logarithm — not a logarithm in an arbitrary base, which is a different search intent served by a general log-base-b calculator, and not a discrete logarithm modulo a prime.
What to enter and what comes back
On success the body echoes the parsed value, returns log2 as the binary logarithm rounded to twelve decimal places (exact integer powers of two, including unit fractions such as one half and one fourth, stay exact integers), and includes a short expression string plus the change-of-base formula log2(x) equals ln(x) divided by ln(2) so you can display the identity next to the answer. Example: value equals eight yields log2 equal to three and the expression log2(8) = 3. Example: value equals one half yields log2 equal to minus one.
Example: value equals ten yields a rounded irrational close to 3.321928094887. Failed validation is not billed.
Where a log2 calculator earns its keep
Classrooms and tutoring platforms use a log2 calculator so learners can check the binary logarithm of arbitrary decimals without converting by hand or trusting a phone that silently prints minus Infinity at zero. Computer-science notebooks convert a positive magnitude into bits: the number of bits needed to represent an integer n is one plus the floor of log2 of n, binary-search depth on a sorted array of length n is about log2 of n, and Shannon entropy of a fair coin is one bit.
Networking and systems scripts size address spaces, hash tables, and page counts with the same identity, and they need the inversion to fail loudly when someone pastes a non-positive count rather than to emit an infinite bit width. Keep requests small, pin the rounded logarithm in tests, and you get a reproducible binary log for homework, LMS plugins, and edge workers without standing up symbolic math infrastructure of your own.
What you can do with it
Classroom and homework checks
Let students submit a positive finite number and verify that the returned log2 is the unique exponent y with 2^y equal to that input, including unit fractions and powers of two.
Bit width, search depth, and entropy
Convert a positive magnitude into bits: address width, binary-search depth, or Shannon entropy in bits, with a deterministic log2 that refuses zero and negatives.
Exam and LMS auto-grading
Mark log2 items with an oracle that rejects zero, negatives, and non-finite input instead of returning minus Infinity a buggy grader might accept.
FAQ
How do I compute the base-2 logarithm of a number?
Example: value 8 yields 3, because 2^3 = 8.
Is log2 of one half defined?
Yes. log2(0.5) is minus one, because two to the power of minus one is one half. Any positive unit fraction that is a power of one half returns an exact negative integer.
What happens if the value is missing or not a finite number?
NaN, Infinity, empty strings, and non-numeric values are rejected and are not billed.
What happens if I enter zero or a negative number?
You get a clear message instead of a result. Among the real numbers the base-2 logarithm is not defined at zero or for negatives, so the calculator refuses them rather than showing minus infinity.
What is the base-2 logarithm used for?
Counting bits is the classic use: it tells you how many binary digits a magnitude needs. It also gives information entropy in bits, the depth of a binary search and the size of an address space.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/math/log-2 \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"value":8}'const res = await fetch("https://api.kit.forhosting.com/math/log-2", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"value": 8
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/math/log-2",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"value": 8
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/math/log-2", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"value":8}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"value":8}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/math/log-2", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"value": 8
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "math.log_2",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
Engineers use it for information entropy in bits, binary-search depth, IEEE exponents, and address-space sizing. This calculator accepts a single positive finite number, returns log2 of that input, and fails with invalid_input when the value is missing, zero, negative, or not a finite number.
What the base-2 logarithm is, and when log2(x) is defined
It is undefined at zero and on the negative reals among the real numbers, which is why this calculator treats those inputs as errors rather than returning NaN or minus Infinity from IEEE arithmetic.
Input contract, response shape, and how errors are reported
POST a JSON body with one required positive finite number: value, the number whose base-2 logarithm you want. A JSON number or a numeric string is accepted; missing fields, empty strings, booleans, arrays, objects, NaN, and Infinity fail with invalid_input. Pair the numeric fields with your own copy if you need longhand classroom steps; this endpoint computes the logarithm and does not emit worked solutions beyond the formula and expression strings.
Where a log2 calculator earns its keep
Exam generators need a deterministic oracle that marks zero, negatives, and non-finite inputs as invalid instead of a success payload a buggy grader might treat as a correct blank.
Technical questions
What happens if the input is zero or negative?
The task fails with invalid_input. The real base-2 logarithm is defined only for strictly positive numbers, so the calculator does not return minus Infinity or NaN and the call is not billed.
What does the log2 calculator cost?
The browser tool is free. API requests use prepaid KIT balance at $0.002 per successful call; invalid input is not billed.