Compute two's complement binary
Convert in both directions at a fixed width: -18 in 8 bits is 11101110, and reading 11101110 at 8 bits gives -18 again, with widths from 2 to 53 bits and no silent wrapping. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Two's complement is the standard fixed-width representation used for signed integers in processors, file formats, registers, and network protocols. This converter handles both directions: provide an integer and a bit width to obtain the exact binary pattern, or provide a binary pattern and its width to recover the signed integer.
Choose the width before converting
A two's complement value has meaning only when its width is known. The same trailing bits may represent different values when interpreted as an eight-bit, sixteen-bit, or thirty-two-bit word, because the leftmost bit of the selected width acts as the sign indicator. Start by choosing a width from 2 through 53 bits, then select the conversion direction.
For integer-to-binary conversion, enter a signed whole number. The converter checks the legal interval, which runs from negative two raised to bits minus one through positive two raised to bits minus one, minus one. An eight-bit word therefore accepts values from -128 through 127.
A successful result always contains exactly the number of digits requested, including leading zeroes for nonnegative values and leading ones when required for a negative fixed-width representation.
Understand how encoding and decoding work
For a nonnegative integer, encoding is ordinary base-two notation padded on the left with zeroes until it reaches the selected width. For a negative integer, the converter adds the value to two raised to the width and writes that nonnegative residue in binary. This modular definition is equivalent to the familiar manual procedure of inverting all bits of the positive magnitude and adding one, but it avoids special cases and states the mathematics directly.
Decoding reverses the rule. The converter first reads the bit string as an unsigned quantity. When its leading bit is zero, that quantity is already the signed answer.
When its leading bit is one, the converter subtracts two raised to the width to recover the negative value. Binary input must contain only zeroes and ones and must have exactly the declared number of characters. Width is never guessed, padding is never inserted during decoding, and extra digits are never truncated.
Those choices make round trips predictable: every accepted integer encodes to one exact pattern, and decoding that pattern at the same width restores the original integer.
Use exact representations in real workflows
This capability is useful anywhere a human-readable decimal value must be compared with a machine-level signed field. Firmware developers can verify sensor deltas, status registers, and protocol examples before copying them into documentation. Students can check their work while still seeing the exact fixed-width pattern rather than a shortened binary value that hides sign extension.
The computation is deterministic, local to the parse worker, and does not use a model, external service, random source, or clock. Keep the declared width consistent with the storage format you are examining. If a protocol says a field is twelve bits, decode all twelve digits, including leading zeroes or ones.
Changing the width changes the sign position and can legitimately change the interpreted number even when the visible suffix remains identical.
What you can do with it
Check signed register values
Convert documented decimal limits and examples into exact-width bit patterns for firmware and hardware reviews.
Decode protocol captures
Interpret a fixed-width binary field as the signed integer intended by a protocol specification.
FAQ
What range fits in a given width?
A width of w bits stores signed integers from -2^(w-1) through 2^(w-1)-1.
Why must binary input match the width exactly?
The leading bit at that exact width determines the sign, so guessing or silently padding the width could change the result.
What happens when an integer does not fit the width?
You get a clear error instead of a wrapped or cut-down result. An 8-bit word, for example, accepts only -128 through 127, so a value outside that range is refused rather than losing its high bits.
How is a negative number encoded?
The value is added to two raised to the width, and that result is written in binary. It is the same as inverting all the bits of the positive number and adding one, which is why -18 in 8 bits becomes 11101110.
How do I turn a bit pattern back into a number?
The bits are first read as an unsigned number. If the leftmost bit is zero, that is already the answer; if it is one, two raised to the width is subtracted, so 11101110 at 8 bits gives -18.
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/dev2/two-complement-convert \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"mode":"integer_to_binary","bits":8,"integer":-18,"binary":"11101110"}'const res = await fetch("https://api.kit.forhosting.com/dev2/two-complement-convert", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"mode": "integer_to_binary",
"bits": 8,
"integer": -18,
"binary": "11101110"
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/dev2/two-complement-convert",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"mode": "integer_to_binary",
"bits": 8,
"integer": -18,
"binary": "11101110"
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/dev2/two-complement-convert", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"mode":"integer_to_binary","bits":8,"integer":-18,"binary":"11101110"}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"mode":"integer_to_binary","bits":8,"integer":-18,"binary":"11101110"}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/dev2/two-complement-convert", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"mode": "integer_to_binary",
"bits": 8,
"integer": -18,
"binary": "11101110"
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "dev2.two_complement_convert",
"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.
Limits
min_bits | 2 |
max_bits | 53 |
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
It validates every request rather than silently wrapping values that do not fit.
Choose the width before converting
If the integer falls outside that interval, the request returns an input error instead of discarding high bits or wrapping into a misleading result. This strict behavior is valuable when checking register definitions, serializer boundaries, classroom exercises, and data layouts.
Use exact representations in real workflows
For automation, send the mode, width, and relevant integer or binary field through the API; each successful item uses the published base price of $0.002.
Integration use cases
Build serializer test fixtures
Generate stable binary expectations for positive, negative, minimum, maximum, and zero values.
Technical questions
What happens when an integer does not fit?
The conversion fails with an invalid_input error that states the allowed range. It never wraps or truncates the value.
Can this convert in both directions?
Yes. Use integer_to_binary to encode a signed integer and binary_to_integer to decode a fixed-width bit pattern.
How much does an API request cost?
Each successful API item uses the published base price of $0.002. Invalid input is rejected before a valid result is produced.