Encode Negative Binary

Encode negative numbers in binary using two's complement, one's complement, or sign-magnitude, with the bit width you choose. Free and in-browser.

Convert negative decimal integers to two's complement binary at 8, 16, 32, 64, or any custom bit width up to 256. Optional step-by-step view shows the magnitude → invert → +1 derivation. BigInt under the hood, so arbitrary widths work.

How to Use Encode Negative Binary

  1. Enter negative integers (one per line) in the input box.
  2. Pick a bit width - 8, 16, 32, 64, or Custom (4-256). The lower bound on representable values is −2 to the (N−1); going below it triggers a per-line range error.
  3. Optionally enable "Show encoding steps" to display the derivation: take the absolute value's binary, invert every bit, add 1.
  4. Click Convert or press Ctrl/Cmd + Enter. The output box shows one row per input with the sign bit highlighted on the left.
  5. Copy or download as twos-complement.txt.

Frequently Asked Questions

Does this tool handle positive numbers too?

No. The tool is named “Negative Binary Encoder” and is restricted to strictly negative integers. An older version of this FAQ claimed positive numbers worked – that was incorrect; the code throws on zero or positive input. If you want to encode positive integers as binary, use a regular decimal-to-binary tool; if you want to see how positive numbers fit into the same two’s complement bit pattern, just take the ordinary binary of the positive value and pad to the bit width.

What is two’s complement?

Two’s complement is the dominant way to represent negative integers in binary on modern CPUs. For an N-bit field, the value −x is encoded as the N-bit pattern that, when added to x (mod 2 to the N), gives zero. Practically: take the binary of |x|, invert every bit, then add 1. Examples at 8 bits: −1 → 11111111, −128 → 10000000.

Why does the sign bit matter?

In two’s complement, the leftmost (most-significant) bit doubles as a sign indicator: 1 means the value is negative, 0 means non-negative. This tool highlights the sign bit in indigo so you can quickly verify the sign of a freshly computed pattern. Note that the sign bit isn’t a “separate” bit added on top – it’s just the top bit of the N-bit field, and it carries arithmetic weight equal to −2 to the (N−1).

What’s the representable range at each bit width?

For N bits, two’s complement covers [−2^(N−1), 2^(N−1) − 1]. So 8-bit covers −128…127; 16-bit covers −32,768…32,767; 32-bit covers about ±2.1 × 10⁹; 64-bit covers about ±9.2 × 10¹⁸. The tool rejects any input below the lower bound for the chosen width – try a wider width or scale your problem.

What does the “show steps” toggle do?

When on, each row expands to show the three classical steps: (1) the bit pattern of the absolute value, (2) that pattern with every bit inverted (the “one’s complement”), and (3) adding 1 to get the two’s complement. Helpful for teaching, debugging, and sanity-checking your hand calculations.

Why use BigInt instead of plain Number?

JavaScript Numbers are 64-bit floats with only 53 bits of integer precision. For 53-bit-and-larger widths (e.g. 64-bit signed integers like the minimum −2⁶³), Number would silently lose precision. BigInt is unbounded, so 64-bit values are exact and arbitrary widths up to 256 bits work without precision loss.

Is anything sent to a server?

No. The page loads three static files (HTML, CSS, JS) and then runs entirely in your browser. You can disconnect from the internet after the page loads. No analytics, no tracking, no cookies.

Is this tool free?

Yes – free, unlimited, no signup, no watermark. Use the output in any context. Attribution to is appreciated but not required.