Bits Needed for Values Calculator
This bits needed for values calculator finds the smallest binary width that can hold either a specified number of distinct states or every non-negative integer through a specified maximum.
Run — free
It handles exact powers of two, boundary values, and zero explicitly, then reports both the required bit count and the range that width can represent. Use it when selecting register widths, sizing encoded fields, planning digital interfaces, or checking whether a compact integer format has enough capacity.
Choose the input that matches your requirement
Start by deciding whether your requirement is a count of possibilities or a largest numeric code. Use distinct_values when the things being encoded are states, categories, symbols, device modes, or any other set whose members merely need unique codes. For example, a selector with twelve positions has twelve distinct values even if its labels are not numbers. Use max_value when the codes are non-negative integers and every integer from zero through a known upper bound must fit. A field that stores readings from 0 through 1000 therefore has a maximum value of 1000 and represents 1001 possible integers. Provide exactly one input so the intended interpretation is unambiguous. The calculator accepts only non-negative safe integers, avoiding fractions, rounded values, and numeric inputs that JavaScript cannot preserve exactly. This distinction prevents the common off-by-one mistake of treating a maximum value as though it were already a count.
Understand how the minimum width is found
A binary field with b bits has 2 raised to the power b different bit patterns. The minimum width is therefore the smallest b whose capacity is at least the required number of patterns. The implementation finds that point by starting with one pattern and repeatedly doubling capacity, which makes exact power-of-two boundaries reliable without depending on floating-point logarithm rounding. If you request 8 distinct values, 3 bits are enough because they provide exactly 8 patterns. If you request 9, the result becomes 4 bits and the capacity becomes 16. For a maximum integer, zero is included, so the required pattern count is the maximum plus one. A maximum of 255 fits in 8 bits, while 256 needs 9. One distinct abstract state requires zero information bits; by contrast, the integer value zero is reported as a conventional 1-bit representation so a stored binary field still has a physical width.
Apply the result without overlooking system constraints
Use the returned bits value as the mathematical minimum, then compare it with the widths your actual system supports. Hardware registers, instruction sets, network protocols, databases, and serialization formats often offer fixed widths such as 8, 16, 32, or 64 bits, so you may need to round upward to an available container. The response also includes representable_values and maximum_representable_value, which make spare capacity visible and help reviewers confirm the boundary. The calculation assumes unsigned, contiguous codes beginning at zero. Signed integers, reserved sentinel codes, parity bits, error-correction bits, alignment padding, and protocol flags consume capacity or follow different representation rules; include those requirements before choosing the final field. This calculator provides the exact lower bound for the stated value set, not a complete storage-layout decision. It runs deterministically without network access, and browser use is free; automated API requests use the published $0.002 base price.
What you can do with it
Size a digital selector field
Find the smallest control-field width that assigns a unique code to every operating mode or switch position.
Check an unsigned sensor range
Confirm how many bits are required to encode every integer reading from zero through the sensor's documented maximum.
Plan compact identifiers
Estimate the minimum binary width for a fixed set of categories before selecting a practical byte-aligned storage type.
FAQ
What is the formula for a count of distinct values?
The minimum is the ceiling of log base 2 of the count. The calculator uses exact iterative doubling to avoid rounding errors at boundaries.
Why does a maximum value require adding one?
A non-negative range includes zero. Values from zero through M contain M plus one distinct integers.
Why does one distinct value need zero bits?
No choice must be encoded when only one state exists, so its information content is zero bits. A real format may still allocate storage.
Does the result include a sign bit?
No. The maximum-value mode covers unsigned non-negative integers. Signed representations require a separate range calculation.
Can I enter both fields?
No. Provide exactly one field so the calculator does not have to guess whether your requirement is a count or an inclusive maximum.
What does the API request cost?
The published base price is $0.002 per request. You can also run the same deterministic calculation free in your browser.
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/elec/bits-for-values \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"distinct_values":1000}'const res = await fetch("https://api.kit.forhosting.com/elec/bits-for-values", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"distinct_values": 1000
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/elec/bits-for-values",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"distinct_values": 1000
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/elec/bits-for-values", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"distinct_values":1000}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"distinct_values":1000}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/elec/bits-for-values", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"distinct_values": 1000
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "elec.bits_for_values",
"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. |