Two's Complement Calculator

Two's Complement Calculator

Convert a signed decimal integer to a fixed-width two's complement bit pattern, or decode a binary pattern back to its signed decimal value.

Width 8 bits Signed value – 37 Sign Negative Hex 0xDB

The startup example is ready to export as a validated XLSX workbook.

Inputs

The selected width controls the signed range and the number of output digits.

Enter a whole base-10 number. For 8 bits, the allowed range is – 128 to 127.

Enter up to 8 binary digits. Spaces are allowed; leading zeros are optional.

Live result

Two's complement binary 1101 1011
Signed decimal – 37
Unsigned encoding 219
Hex encoding 0xDB
Representable range – 128 to 127
Sign bit 1
Magnitude 37

8-bit two's complement 11011011 represents negative 37.

Signed bit contribution

Bit position Digit Signed weight Contribution
7 (sign) 1 – 128 – 128
6 1 64 64
5 0 32 0
4 1 16 16
3 1 8 8
2 0 4 0
1 1 2 2
0 1 1 1

Add the contributions to get – 37. In two's complement, the most significant bit has weight – 2⁷ for an 8-bit value.

How to use the two's complement calculator

What this calculator does

This calculator translates between a signed base-10 integer and its fixed-width two's complement binary representation. It also exposes the same stored bits as an unsigned integer and hexadecimal value, then breaks the signed result into per-bit contributions. It is useful for checking low-level data, learning signed binary notation, reviewing register or protocol values, and confirming how a bit pattern will be interpreted at a chosen width. It does not simulate a particular processor, programming-language overflow rule, byte order, or floating-point format; it handles exact signed integers at 4, 8, 16, or 32 bits.

When to use it

Use it when you need to encode a negative integer for a byte or word, decode a binary dump, compare signed and unsigned readings of the same bits, or verify a classroom conversion by hand. The underlying “invert the bits and add one” method is illustrated in Cornell's two's complement notes.

How to calculate

  1. The calculator opens with a complete 8-bit example: decimal – 37 and binary 11011011. Its results and validated XLSX workbook are immediately available.
  2. Choose Binary number representation. Changing the width changes the legal signed range and the required padded output length.
  3. Edit either Decimal integer or Binary pattern. The most recently edited field becomes the source, and a valid entry updates the other field automatically.
  4. Read Two's complement binary first, then use the secondary cards and the Signed bit contribution table to inspect the same value in other forms.
  5. Select Download Excel to export the current canonical values and every bit row. Select Reset to clear the demonstration data. Reset intentionally leaves the calculator empty and disables export until a complete valid entry is supplied again.

Input guide

Binary number representation is required and accepts 4-bit, 8-bit, 16-bit, or 32-bit. For example, choose 8-bit for one byte. A wider setting expands the range and adds leading sign-preserving digits; a narrower setting can make the current decimal invalid. A common mistake is choosing a width that does not match the source data.

Decimal integer is a required whole base-10 value when it is the active source. Enter optional leading minus or plus signs followed by digits, such as – 37. Decimal points, grouping commas, and scientific notation are rejected. The value must lie from – 2n – 1 through 2n – 1 – 1 for an n-bit width. More negative values generally produce leading 1s; positive values produce a leading 0. Do not confuse the signed decimal range with the larger unsigned range.

Binary pattern is a required sequence of 0s and 1s when it is the active source. Spaces may separate groups, so both 11011011 and 1101 1011 are accepted. Leading zeros are optional and the calculator pads to the selected width. The pattern may not exceed that width. Its leftmost padded bit is the sign bit, so entering 1111 at 4 bits means – 1, not unsigned 15.

Output guide

Two's complement binary is the exact fixed-width bit pattern. Signed decimal is the interpreted integer. Unsigned encoding treats the identical bits as a nonnegative base-10 number, while Hex encoding expresses those bits compactly in base 16. Representable range states the legal signed bounds for the selected width. Sign bit is 0 for zero or a positive value and 1 for a negative value. Magnitude is the absolute decimal size, with the caveat that the most negative value has no positive counterpart at the same width.

The summary pills repeat Width, Signed value, Sign, and Hex from the same canonical model. In the table, Bit position counts from the least significant bit at position 0; Digit is the stored 0 or 1; Signed weight is the positional value, with the top bit negative; and Contribution is the digit multiplied by that weight. Their sum is an exact identity, not an estimate.

Worked example

With 8 bits and decimal – 37, the modulus is 2⁸ = 256. The stored unsigned encoding is 256 – 37 = 219. Converting 219 to binary gives 11011011, and to hexadecimal gives 0xDB. Reading the signed weights gives – 128 + 64 + 16 + 8 + 2 + 1 = – 37. Those are the exact values shown on first open and written to the startup workbook.

How the signed range and conversion work

An n-bit pattern has 2n possible combinations. Two's complement assigns the lower half to 0 through 2n – 1 – 1 and the upper half to – 2n – 1 through – 1. That produces one zero and one extra negative value. The general signed range is:

minimum = – 2^(n – 1) maximum = 2^(n – 1) – 1

For a nonnegative decimal value, the fixed-width representation is simply its ordinary binary digits padded with leading zeros. For a negative value x, the stored unsigned code is 2n + x. This is equivalent to writing the positive magnitude, flipping every bit, and adding one. The broader mathematical interpretation and sign-extension behavior are summarized in the two's complement reference article.

To decode a pattern, first read it as an unsigned integer u. If the sign bit is 0, the signed value is u. If the sign bit is 1, subtract 2n. For example, 11011011 is unsigned 219; 219 – 256 = – 37.

Interpretation tips and common mistakes

  • Width is part of the value. Binary 1011 is – 5 at 4 bits, but positive 11 when zero-extended to 8 bits as 00001011. Sign-extending a negative value repeats the leading 1 instead.
  • Signed and unsigned are interpretations. The bits do not change when you switch from – 37 to unsigned 219 or hex 0xDB; only the meaning changes.
  • Overflow cannot be ignored. Decimal 128 does not fit in signed 8-bit two's complement even though 128 fits in an unsigned byte.
  • Bitwise operators may coerce widths. Programming languages often apply their own integer-width rules. For JavaScript specifically, review MDN's explanation of bitwise operator coercion and 32-bit operands before comparing code output.

The contribution table is often the fastest audit method. If the sign bit is 1, begin with the negative top weight and add every lower positive weight whose digit is 1. This directly yields the signed decimal value without first inverting the pattern.