ForHosting KIT · Developer Utilities

Negabinary converter

13 is 11101 in negabinary, base −2, whose places alternate 1, −2, 4, −8 and 16, so every whole number, negative ones included, is written with only 0 and 1 and no minus sign. Convert decimal to negabinary and back. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Value
13
Direction
To negabinary
You get
Result
11101
Decimal
13
Negabinary
11101
Alphabet
01

A negabinary converter rewrites a signed whole number between everyday base-10 and base −2, the positional system whose radix is negative two and whose only digits are zero and one. Because successive powers of −2 alternate between positive and negative values, every integer—positive, negative, or zero—has a unique representation without a dedicated sign bit or two’s-complement width. Students of computer arithmetic, puzzle authors, and engineers exploring exotic number systems can paste a decimal value or a bit string, choose encode or decode, and receive a deterministic result with the cleaned decimal integer, the negabinary string, the digit count, and the fixed alphabet. Invalid digits, empty input, and non-integers fail with a clear invalid-input error instead of a silent wrong answer.

How negabinary (base −2) encoding and decoding work

Negabinary is positional notation with radix minus two. Each place is a power of −2: the units place is (−2) to the zero, which is one; the next place is −2; then four; then −8; then sixteen; and so on, alternating in sign as the exponent grows. The only digit symbols are zero and one—the same alphabet as ordinary binary—but the alternating place signs mean a string of ones and zeros can evaluate to a negative total without any leading minus character.

Classic checks include two encoding to one-one-zero (because four minus two equals two), four encoding to one-zero-zero, and negative one encoding to one-one (because minus two plus one equals minus one). To encode a signed decimal integer, repeatedly take the remainder when dividing by −2 and adjust so every remainder is non-negative: if the remainder would be −1 under truncating division, add two to the remainder and carry one into the next quotient.

Digits are collected least-significant first and reversed for the usual left-to-right string, with leading zeros stripped except for the number zero itself. To decode, walk the string from left to right with a Horner-style accumulation: multiply the running total by −2 and add the next digit. An optional 0nb or 0NB prefix is stripped the same way binary tools accept 0b, so scripts can tag base-minus-two literals clearly.

An invalid character such as a space, plus or minus sign inside the bit string, digit two or higher, letter, or punctuation mark aborts with invalid input naming the bad character and its position. Absolute values must stay inside JavaScript’s safe integer range so every digit remains exact when the result is serialized. If you only need ordinary positive-base binary or two’s-complement fixed-width encodings, those are separate search intents with their own landings rather than stretching this signed base-minus-two converter past its honest scope.

Where a negabinary converter earns its keep

Computer science courses introduce negative bases to show that sign bits and two’s complement are design choices rather than laws of nature: once students convert a few negative integers by hand, they see how alternating place values absorb the sign into the digits themselves. Exam-prep generators and LMS plugins need a deterministic oracle that marks wrong an ordinary binary decode of a string that was meant as negabinary, and that never invents a leading minus on a valid bit string.

Game designers invent alien numbering systems with no sign glyph and need deterministic conversion for lore tools and save formats. Treat ordinary binary or hexadecimal dumps, fixed-width two’s complement, Gray code, and multi-base radix tables that stop at thirty-six as separate search intents with their own landings rather than stretching this integer negabinary converter past its honest scope.

Negative-base arithmetic lessons

Convert homework integers between base-10 and base −2 so students see how alternating place signs encode negatives without a sign bit.

Exam and LMS auto-grading

Mark negabinary encode/decode items with a deterministic oracle that rejects invalid digits and never invents a leading minus.

What is negabinary?

Negabinary is base −2: positional notation with radix minus two and digits 0 and 1 only. Powers of −2 alternate in sign, so every integer has a unique representation without a dedicated sign bit.

Why is there no minus sign on negative results in the bit string?

Negative place values (…, 16, −8, 4, −2, 1) already encode the sign. A string such as 11 means −2 + 1 = −1.

How do I convert a decimal number to negabinary by hand?

Divide by −2 again and again, keeping each remainder as 0 or 1: if a remainder would be −1, add 2 to it and carry 1 into the quotient. Then read the remainders from last to first; 13 gives 11101.

How do I convert negabinary back to decimal?

Add up the place values where a 1 appears, starting from the right: 1, −2, 4, −8, 16 and so on. For 11101 that is 16 − 8 + 4 + 1, which makes 13. You can also multiply the running total by −2 and add each digit from left to right.

Can I convert fractions or very large numbers?

No. Only whole numbers are accepted, with an optional leading plus or minus; fractions and scientific notation are rejected. Values up to 9007199254740991 in absolute size work, so every digit of the result stays exact.

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.

POSThttps://api.kit.forhosting.com/enc/base-negabinary

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.

curl -X POST https://api.kit.forhosting.com/enc/base-negabinary \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"value":"13","direction":"to_negabinary"}'
{
  "value": "13",
  "direction": "to_negabinary"
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "enc.base_negabinary",
  "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.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Pricing for successful API calls is the published base of $0.002 per item.

How negabinary (base −2) encoding and decoding work

The algorithm uses exact integer arithmetic only: no floating-point rounding, no random seeds, no wall-clock timestamps, and no network calls, so identical inputs always produce identical JSON.

What you send and what you get back

Send two required fields: value and direction. Absolute decimal values larger than Number.MAX_SAFE_INTEGER are rejected so low-order digits cannot silently disappear. Classic round-trips include thirteen encoding to one-one-one-zero-one and decoding back to thirteen, and negative five encoding to one-one-one-one. Direction must be to_negabinary when value is a signed decimal integer, or from_negabinary when value is a base −2 bit string. Aliases are accepted so forms and scripts can use the names that fit their UI: value also accepts n, number, input, text, decimal, negabinary, bits, and base_neg2; direction also accepts dir, mode, and op, plus short synonyms such as to, from, encode, decode, to_base_neg2, from_base_neg2, decimal_to_negabinary, and negabinary_to_decimal. For to_negabinary, value may be a JSON integer or a digit string with an optional leading plus or minus; fractions, scientific notation, bare words, NaN, Infinity, and empty strings fail with invalid_input and are not charged. For from_negabinary, every character must be zero or one after an optional 0nb prefix; any other character—including two, three, underscore, hyphen, space, and a leading sign—fails with a message that names the invalid negabinary digit and its zero-based position. On success the response echoes the cleaned value string, the normalized direction, decimal as a number (negative when appropriate), negabinary as the compact base −2 string, result as the primary converted form (negabinary when encoding, decimal string when decoding), the alphabet string 01, base set to −2, and digit_count for the negabinary representation. Pricing for successful API calls is the published base of $0.002 per item; validation failures do not consume prepaid balance.

Where a negabinary converter earns its keep

Puzzle and competitive-programming authors set challenges that require printing or parsing base −2 without calling a full computer-algebra system; a tiny parse API supplies expected values for sample tests and private keys. Because this capability is a pure parse engine, API pricing is the published base of $0.002 per successful item and failed validation does not consume balance.

Puzzle keys and unit-test fixtures

Generate expected base −2 strings for competitive-programming samples and multi-base UI tests, including negative edge cases.

How do I convert a decimal integer to negabinary?

Send direction to_negabinary and value as a signed whole number (JSON integer or digit string with optional +/−). The API returns the base −2 bit string in the negabinary and result fields.

How do I decode a negabinary string back to decimal?

Send direction from_negabinary and value as a string of digits 0 and 1 only (optional 0nb prefix). The API returns the signed decimal integer; do not include a leading + or − on the bit string.

What does the negabinary converter cost?

The browser tool is free. API requests use prepaid KIT balance at $0.002 per successful call; invalid input is not billed.

Read the full KIT documentation →