What two’s complement represents
Two’s complement is the standard way most computers store signed integers. A fixed-width register has a finite set of bit patterns. The lower half represent non-negative values, while patterns with the top bit set represent negative values. In eight bits, 01111111 is 127, 10000000 is -128, and 11111111 is -1. The same physical bits can also be read as unsigned values from 0 through 255, so width and interpretation are essential context.
This converter keeps those concepts separate. Signed decimal mode checks the legal signed range and produces the corresponding pattern. Unsigned decimal mode treats the input as the raw magnitude held by all bits. Bit-string mode requires exactly the selected number of bits and reports both interpretations. Nothing is rounded, truncated, or silently wrapped when a decimal input is outside the valid range.
How to use the two’s-complement converter
Choose the interpretation before entering a value; the same digits can mean different things under different modes.
- Select Signed decimal, Unsigned decimal, or Exact bit string according to the source you are analyzing.
- Choose 8, 16, 32, or 64 bits. This determines the legal ranges, padding length, sign bit, and hexadecimal width.
- Enter the value. Decimal modes accept a whole integer; bit mode accepts only 0 and 1 and requires exactly the selected width.
- Press Calculate to see the bit pattern, padded hexadecimal, signed decimal interpretation, and unsigned decimal interpretation.
- When copying a result into code or a debugger, preserve the width. Removing leading zeros can erase the information needed to identify the sign bit.
Two’s-complement examples
The examples below use eight bits unless another width is stated.
| Input | Bit pattern | Explanation |
|---|---|---|
| 42 signed, 8-bit | 00101010 | Positive values use their ordinary binary magnitude with leading zero padding. |
| -42 signed, 8-bit | 11010110 | Invert 00101010 and add one, producing the fixed-width representation of -42. |
| -1 signed, 8-bit | 11111111 | All ones represents -1 at every standard two’s-complement width. |
| 128 unsigned, 8-bit | 10000000 | The raw pattern is valid unsigned 128 but represents signed -128. |
| 32767 signed, 16-bit | 0111111111111111 | This is the largest signed 16-bit integer because the top bit remains zero. |
| -9223372036854775808 signed, 64-bit | 8000000000000000 hex | The minimum signed 64-bit value has only the sign bit set. |
Widths, ranges, and accepted input
For a width of n bits, the signed range is -2ⁿ⁻¹ through 2ⁿ⁻¹-1. The unsigned range is 0 through 2ⁿ-1. Eight bits therefore allow signed -128 to 127 or unsigned 0 to 255. Sixty-four bits allow signed -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, while the unsigned maximum is 18,446,744,073,709,551,615.
Decimal inputs may contain a sign plus spaces or underscores used as separators. Binary input may use a 0b prefix and visual spaces or underscores, but after formatting is removed it must contain exactly the selected width. Exact length prevents accidental sign extension or zero extension.
- Signed decimal mode rejects values outside the signed range rather than wrapping them.
- Unsigned decimal mode rejects negative numbers and values larger than the all-ones pattern.
- Bit mode preserves leading zeros and always reports both signed and unsigned meanings.
- Hex output is padded to 2, 4, 8, or 16 digits for 8, 16, 32, or 64 bits.
How two’s-complement conversion works
A non-negative signed value is stored as ordinary binary as long as it fits below the sign-bit boundary. A negative value x is mapped to 2ⁿ + x, where n is the width. For example, -42 in eight bits becomes 256 - 42 = 214, and 214 in binary is 11010110. This is equivalent to inverting the positive pattern and adding one.
To decode a bit pattern, the tool first reads it as an unsigned integer. If the top bit is zero, the signed value is the same. If the top bit is one, it subtracts 2ⁿ. Thus unsigned 214 becomes signed 214 - 256 = -42 in eight bits.
The arithmetic uses BigInt, including for 64-bit boundaries that cannot be represented exactly by JavaScript Number. Masks and padding are based on the selected width, so output remains deterministic across browsers.
Practical uses
Two’s complement appears anywhere fixed-width signed integers cross a boundary between raw bytes and human-readable values.
Debug registers and memory
Interpret a byte, word, or machine register shown by a debugger as both the signed value and its unsigned raw pattern.
Decode binary protocols
Verify signed sensor readings, counters, audio samples, and fields in network or device protocols with documented widths.
Write boundary tests
Generate exact patterns for minimum, maximum, -1, zero, and sign transitions in parsers, serializers, and database adapters.
Learn integer overflow
See why adding one to a maximum signed pattern changes the sign bit when hardware wraps within a fixed register.
Common mistakes and edge cases
Most errors come from omitting width or mixing signed and unsigned interpretations.
Using the wrong width
11111111 is -1 at eight bits, but if zero-extended to sixteen bits it becomes positive 255. Always keep the original field width.
Entering an out-of-range decimal
Signed 128 does not fit in eight bits, even though unsigned 128 does. Select the correct interpretation instead of expecting silent wrapping.
Dropping leading zeros
A shortened bit string may still have the same unsigned magnitude but no longer identifies the selected fixed-width representation.
Confusing sign-magnitude or one’s complement
Other historical signed encodings use different negative patterns. This tool implements only modern two’s complement.
How this differs from related tools
Two’s complement is about interpretation within a width, not merely changing the printed base.
Decimal to binary
A basic converter may print -42 as -101010. Two’s complement instead produces the actual finite-width stored pattern such as 11010110.
Bitwise calculator
The bitwise calculator applies operations and masks. This page focuses on converting one value and exposing both signed and unsigned meanings.
IEEE-754 converter
IEEE-754 represents floating-point values with sign, exponent, and fraction fields. It is not two’s-complement integer storage.
Octal or hex converter
Base converters preserve mathematical value. Two’s-complement decoding can change the displayed signed value based on the top bit and width.