Short answer: at width n, unsigned values range from 0 through 2n−1 and two’s-complement signed values range from −2n−1 through 2n−1−1. Decode a set-high-bit pattern by subtracting 2n from its unsigned value. Encode negative x as 2n+x. Preserve the high bit when widening, and treat signed overflow separately from carry.
The same bits can have signed and unsigned meanings
An eight-bit register can store 256 patterns. Unsigned interpretation maps them to 0 through 255. Two’s-complement interpretation maps the first half to 0 through 127 and the second half to −128 through −1. Storage does not change; only the interpretation does.
| 8-bit pattern | Unsigned | Signed two’s complement |
|---|---|---|
00000000 | 0 | 0 |
01111111 | 127 | 127 |
10000000 | 128 | −128 |
11111011 | 251 | −5 |
11111111 | 255 | −1 |
Calling the high bit a “sign bit” is convenient but incomplete. Its weight in an n-bit two’s-complement value is −2n−1, while the remaining positions retain positive powers of two. The pattern is not sign-and-magnitude, and simply clearing the high bit does not recover a negative value's magnitude.
Width defines the range and modulus
The bit width identifies which position is highest and establishes arithmetic modulo 2n. Without width, a leading string of ones can continue indefinitely under a mathematical signed model. Serialized fields, registers, file formats, language types, and network protocols must provide the boundary.
| Width | Unsigned range | Signed range | −1 pattern |
|---|---|---|---|
| 8 bits | 0…255 | −128…127 | FF |
| 16 bits | 0…65,535 | −32,768…32,767 | FFFF |
| 32 bits | 0…4,294,967,295 | −2,147,483,648…2,147,483,647 | FFFFFFFF |
| 64 bits | 0…264−1 | −263…263−1 | FFFFFFFFFFFFFFFF |
The signed range contains one more negative value than positive values because zero occupies a nonnegative pattern. Negating the minimum value cannot fit at the same width: positive 128 is outside the signed 8-bit range, just as positive 231 is outside signed 32-bit range.
Decode with unsigned value minus the modulus
Read the complete pattern as unsigned value u. If the high bit is clear, the signed value is also u. If the high bit is set, subtract 2n. For 8-bit 11111011, unsigned u is 251, so the signed value is 251 − 256 = −5.
if u < 2^(n−1): signed = u
otherwise: signed = u − 2^nAn equivalent weighted evaluation assigns −2n−1 to the high bit. For 11111011, calculate −128 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = −5. Both methods require all eight positions.
Encode a negative value modulo 2 to the width
First verify that x falls within the chosen signed range. If x is nonnegative, convert it normally and left-pad with zeroes to n bits. If x is negative, calculate 2n+x and render that nonnegative result in exactly n bits.
2^8 + (−5) = 256 − 5 = 251
251 decimal = 11111011 binary = FB hexThe familiar invert-and-add-one procedure is equivalent: write positive 5 as 00000101, invert within eight bits to 11111010, then add one to get 11111011. “Within eight bits” is essential. Inverting an unbounded mathematical integer is not the same pencil-and-paper operation.
Sign extension preserves value when widening
To widen a two’s-complement value, copy the high bit into every new leading position. Eight-bit positive 5 extends from 00000101 to 00000000 00000101. Eight-bit −5 extends from 11111011 to 11111111 11111011. Zero-extending the latter would produce positive 251 at 16 bits.
Truncation discards leading bits and is only value-preserving when all removed bits are valid sign extension of the retained result. Converting 16-bit 00000001 00101100 (300) to eight bits leaves 00101100 (44), so the value changed. A safe converter should report that 300 does not fit rather than silently mask it unless wrapping is explicitly requested.
Signed overflow and unsigned carry answer different questions
Fixed-width addition computes the low n bits modulo 2n. Unsigned overflow can be indicated by a carry out. Signed overflow occurs when adding two values with the same sign produces a result with the opposite sign.
01111111 (+127)
+ 00000001 ( +1)
= 10000000 (−128 if interpreted as signed)There is no carry out in this example, yet signed overflow occurred. Conversely, 11111111 + 00000001 = 00000000 has a carry out; interpreted as −1 + 1, the signed result zero is valid. Arithmetic flags and application checks must match the chosen interpretation.
Hexadecimal makes fixed widths easier to audit
One hex digit is four bits, so 8, 16, 32, and 64-bit fields require 2, 4, 8, and 16 hex digits. Eight-bit −5 is FB; 16-bit −5 is FFFB. The extra FF is sign extension, not a different mathematical negative.
Byte order is another layer. A 16-bit pattern FFFB can serialize as bytes FF FB in big-endian order or FB FF in little-endian order. Endianness changes byte sequence order, not the abstract integer. Use the Hex Converter only after separating integer width from byte serialization.
Match language APIs without losing precision
ECMAScript defines BigInt.asIntN and BigInt.asUintN as explicit fixed-width mappings. The unsigned operation reduces modulo 2n; the signed operation maps into the corresponding two’s-complement range. Those operations can deliberately wrap, so validate the original value first when overflow should be an error.
Do not parse a long decimal or hex token through floating-point Number before converting it to BigInt. The rounding may already have changed the integer. Preserve the source string and use an exact parser, with an application-level digit bound for memory and responsiveness.
Common signed-binary mistakes
- No width: describing
1111as −1 without saying four bits. - High-bit subtraction only: clearing the high bit instead of subtracting the full modulus.
- Wrong extension: adding zeroes before a negative pattern and changing its value.
- Silent masking: truncating a value that does not fit and presenting the wrapped result as exact.
- Carry equals signed overflow: using the unsigned flag for signed range checks.
- Byte reversal: applying endianness to a written numeral without a fixed-width serialization contract.
Need to inspect one pattern? Select the source base, explicit width, and signed or unsigned interpretation before copying the converted result.
Open the Binary ConverterFrequently asked questions
What is two’s complement?
Two’s complement interprets one of the two-to-the-n fixed-width bit patterns as a signed integer. Values with a clear high bit are nonnegative; values with a set high bit represent the unsigned pattern value minus two to the power n.
Why must two’s complement have a bit width?
Width determines the high bit, the modulus, and the signed range. The pattern 11111111 is minus one at eight bits, but the same eight digits can be the positive prefix of a wider pattern or unsigned 255.
How do I decode a negative two’s-complement value?
Read the n-bit pattern as an unsigned value u. If its high bit is set, subtract two to the power n. For example, 11111011 is unsigned 251, and 251 minus 256 equals minus five at eight bits.
How do I encode a negative number in two’s complement?
First confirm the value fits the selected signed range. For negative x at width n, encode two to the power n plus x, then pad the result to exactly n bits. Invert-and-add-one is an equivalent fixed-width procedure.
What is sign extension?
Sign extension widens a two’s-complement value by copying its high bit into every new leading position. Zeroes extend nonnegative values and ones extend negative values, preserving the signed mathematical value.
How can signed overflow be detected?
For same-width addition, signed overflow occurs when two operands with the same sign produce a result with the opposite sign. A carry out by itself describes unsigned arithmetic and is not the signed overflow test.