Two's complement: decimal to bits and back

Convert decimal to two's complement and back at 4, 8, 16, 32 or 64 bits.

Was this helpful?
Share Tool

This two’s complement calculator converts a signed decimal integer to a fixed-width bit pattern and back. Choose 4, 8, 16, 32 or 64 bits. The signed range for that width is shown under the radios: 8-bit is −128 to 127; 4-bit is −8 to 7; 64-bit uses BigInt so −2^63 is exact. Paste −5 with width 8 and the two’s complement bits are 11111011. One’s complement (the invert of the magnitude) is 11111010. The unsigned readout of those bits is 251. Nothing is uploaded.

A negative integer is stored by padding the magnitude, inverting every bit, and adding 1. Magnitude 5 in 8 bits is 00000101; invert is 11111010; add 1 is 11111011. Positive integers are the magnitude padded with zeros: 127 is 01111111. Zero is all zeros. −1 is all ones at every width (1111 in 4 bits, 11111111 in 8 bits). The decimal to binary converter uses the same invert+1 math on its Number tab; this page is the dedicated width-and-range view, including 4-bit worksheets.

The other direction reads a bit string as signed. 10000000 in 8 bits is −128 because the sign bit weighs −128 and the other bits are 0. 10000001 is −127 (−128 + 1). 01111111 is 127. Short pastes are padded with zeros on the left, so 1011 in 8 bits is +11, not −5. Patterns longer than the width, or a decimal outside the range, show a red alert instead of wrapping. Open Step-by-step working for invert+1 and the sign-bit weight expansion.

Need to add two two’s-complement patterns? Use the binary calculator with steps in signed mode. Need UTF-8 letters? That is the converter’s Text tab. Close this tab when you are done — FlashKit does not store the value.

How to convert decimal and two’s complement

  1. Pick 4, 8, 16, 32 or 64 bits. The signed min and max for that width appear under the radios.
  2. For decimal → bits, type an integer such as −5, 127 or −128. Read two’s complement, one’s complement and the unsigned readout.
  3. For bits → decimal, paste 0/1 (spaces, underscores, optional 0b). The signed value uses the sign-bit weight (−128 for 8-bit MSB).
  4. Open Step-by-step working for invert+1 on negatives, or the place-value expansion of a bit string.
  5. A red alert appears if the value is outside the signed range or the bits are not 0/1. Close the tab when you are done.

Key Features

  • Decimal ↔ two’s complement at 4, 8, 16, 32 and 64 bits
  • Signed range shown per width; BigInt for 64-bit min/max
  • One’s complement and invert+1 steps for negatives
  • Sign-bit weight explanation (8-bit MSB = −128)
  • Red range and invalid-digit alerts; local, no upload

8-bit table (−128, −5, −1, 0, 127)

  • 0 = 00000000. 127 = 01111111 (64+32+16+8+4+2+1). −1 = 11111111. −5 = 11111011. −128 = 10000000.
  • Unsigned readout of −5 is 251, of −1 is 255, of −128 is 128. Those are the same bits read without the sign bit.
  • 128 decimal does not fit in 8-bit signed (max 127). −129 does not fit (min −128). The tool reports the range instead of wrapping to −128.
DecimalTwo’s complement (8-bit)One’s complement of magnitudeUnsigned readout
000000000111111110
1270111111110000000127
−11111111111111110255
−51111101111111010251
−1281000000001111111128

Sign-bit weight, not a minus sign in the bits

  • In two’s complement the leftmost bit is not a printed minus. It contributes −2^(n−1). For 8 bits that is −128; for 4 bits −8; for 16 bits −32768.
  • 10000000 = −128 + 0+0+0+0+0+0+0 = −128. 11111011 = −128 + 64+32+16+8+0+2+1 = −5. 01111111 has a 0 sign bit, so every remaining 1 is a positive place value that sums to 127.
  • 4-bit worksheets: 1000 is −8, 0111 is 7, 1111 is −1. 8 decimal does not fit. The same invert+1 rule applies: magnitude 1 is 0001, invert 1110, add 1 → 1111.
  • 64-bit min is −9223372036854775808, a 1 followed by 63 zeros. JavaScript Number is exact only up to 2^53−1, so many 64-bit values lose precision, which is why this page uses BigInt.

Related tools

  • The decimal to binary converter also shows two’s complement on its Number tab, plus fractions such as 10.625 and UTF-8 text.
  • The binary calculator with steps adds, subtracts, multiplies and divides bit strings, with an optional signed width.

Frequently Asked Questions

What is −5 in 8-bit two’s complement?

11111011. Pad 5 to 00000101, invert to 11111010 (one’s complement), add 1. The unsigned readout of those bits is 251.

Why is 10000000 equal to −128?

The 8-bit sign bit weighs −128. The other seven bits are 0, so the signed value is −128. That is also the 8-bit signed minimum.

What is one’s complement here?

The invert of the padded magnitude. For −5 that is 11111010. Two’s complement is that pattern plus 1. For a positive integer the stored bits are the magnitude; the one’s complement card still shows the inverted magnitude so you can compare.

What ranges are supported?

4-bit: −8 to 7. 8-bit: −128 to 127. 16-bit: −32768 to 32767. 32-bit: −2147483648 to 2147483647. 64-bit: −2^63 to 2^63−1. Values outside that range produce a red error.

Can I paste 0b11111011?

Yes. Spaces, underscores and a 0b prefix are stripped. Other digits show a red invalid-digit alert. Patterns longer than the chosen width are a range error.

Is this the same as the binary converter?

Same invert+1 math, different job. This page is only signed integers at a chosen width, including 4-bit. The converter also does fractions, hex, octal and UTF-8 text.

Expert Guide for Two's Complement Calculator

View Blog
Two's Complement Explained: 8-Bit Examples

Two's Complement Explained: 8-Bit Examples

Learn two’s complement with 8-bit tables: −1, −5, −128 and 127. Invert+1 steps and a calculator that shows the range.

Read Guide

Categories

Browse all FlashKit tools