Two's Complement Calculator
Convert between decimal and two's complement binary at 4, 8, 16 & 32 bits
Last updated September 2026
Method: Two's complement is an exact definition, not an estimate. A value is stored as its residue modulo 2 raised to the bit width, and the leading column carries the negative weight −2 raised to (width − 1). Every pattern shown is computed from that definition.
Included: Decimal to two's complement and back at 4, 8, 16 and 32 bits; the invert-and-add-1 steps; signed, unsigned and hexadecimal readings of the same pattern; a weighted place-value breakdown; range checks; and a signed addition panel that flags overflow.
Not included: Floating-point (IEEE 754) formats, binary fractions, sign-magnitude and one's complement storage, arbitrary bit widths beyond 32, and language-specific behavior such as C's undefined signed overflow.
Signed range: -128 to 127 (256 distinct values)
๐พ Twoโs complement pattern
๐งฎ Step by step
๐ Weighted place values
In two's complement the leftmost column carries a negative weight of -128; every other column is a normal power of two.
| Bit | Weight | Contribution |
|---|---|---|
| position 0 (from left) | -128 | -128 |
| position 1 (from left) | 64 | 64 |
| position 3 (from left) | 16 | 16 |
| position 4 (from left) | 8 | 8 |
| position 6 (from left) | 2 | 2 |
| position 7 (from left) | 1 | 1 |
| Sum | -37 | |
โ Signed addition and overflow
Add two 8-bit signed values and see whether the result still fits.
Exact integer arithmetic. Two's complement is a definition, not an approximation: every pattern above is computed as value modulo 2 to the power of 8, with the leading column weighted -128.
Two's complement: how computers store negative numbers
A two's complement calculator converts a signed decimal number into the bit pattern a processor actually stores, and reads any bit pattern back as a signed value. Example: in 8 bits, −37 is 11011011, which is 0xDB in hex and 219 if the same byte is read as unsigned. One pattern, three legitimate readings.
Two sister tools cover the neighboring questions. The Binary Calculator converts and adds unsigned binary numbers when no negatives are involved, and the Hex Calculator handles hexadecimal arithmetic and hex-to-decimal conversion. Use this page whenever a minus sign, a fixed register width, or a wrap-around result is part of the problem.
What two's complement actually is
A computer register has a fixed number of columns. A byte has eight, a short has sixteen, a typical int has thirty-two. There is no extra place to keep a minus sign, so a convention is needed that lets the same adder circuit handle positive and negative values. Two's complement is that convention, and it is defined by one piece of arithmetic:
pattern = value mod 2n | value = −bn−1 × 2n−1 + ∑ bi × 2i In words: to store a number you take it modulo 2 raised to the bit width, so −37 in a byte becomes 256 − 37 = 219, whose binary form is 11011011. To read a pattern back you use ordinary place values with one twist: the leftmost column counts as negative. In a byte that column is worth −128 instead of +128, and every other column keeps its usual weight of 64, 32, 16, 8, 4, 2 and 1.
The practical shortcut everyone learns is invert and add one, and it produces exactly the same answer as the modulo definition, because flipping all n bits of a number x gives 2n − 1 − x, and adding 1 gives 2n − x. That is the residue of −x. The shortcut is not a separate rule; it is the definition rearranged so it can be done by hand.
Worked example: −37 in an 8-bit register
Start with the magnitude and work down the three steps:
- Write 37 in binary, padded to 8 bits: 37 = 32 + 4 + 1, so the pattern is
00100101. - Invert every bit:
11011010. As an unsigned number that is 218, and 218 = 255 − 37, exactly as the algebra predicts. - Add 1:
11011010+00000001=11011011, which is 219 unsigned and 0xDB in hexadecimal.
Check it with the weighted place values: the bits that are set sit in the −128, 64, 16, 8, 2 and 1 columns, and −128 + 64 + 16 + 8 + 2 + 1 = −37. Check it a second way by adding the pattern to +37: 11011011 + 00100101 = 100000000, and the ninth bit falls off the end of the byte, leaving 00000000. A number that adds to zero is the negative, which confirms the result.
All sixteen 4-bit patterns
Four bits are small enough to list completely, which makes the wrap-around obvious. The patterns run 0 to 7 on the positive side, then jump straight to −8 when the sign bit turns on, and count back up to −1 at all ones:
| Bits | Signed | Unsigned | Hex |
|---|---|---|---|
| 0000 | 0 | 0 | 0x0 |
| 0001 | 1 | 1 | 0x1 |
| 0010 | 2 | 2 | 0x2 |
| 0011 | 3 | 3 | 0x3 |
| 0100 | 4 | 4 | 0x4 |
| 0101 | 5 | 5 | 0x5 |
| 0110 | 6 | 6 | 0x6 |
| 0111 | 7 | 7 | 0x7 |
| 1000 | −8 | 8 | 0x8 |
| 1001 | −7 | 9 | 0x9 |
| 1010 | −6 | 10 | 0xA |
| 1011 | −5 | 11 | 0xB |
| 1100 | −4 | 12 | 0xC |
| 1101 | −3 | 13 | 0xD |
| 1110 | −2 | 14 | 0xE |
| 1111 | −1 | 15 | 0xF |
Two details are worth memorizing from this table. All ones is always −1, at every width. And the signed and unsigned readings of a negative pattern always differ by the full span: 1011 is −5 signed and 11 unsigned, and 11 − 16 = −5.
Ranges by bit width
The range is asymmetric at every width, because zero uses up one of the positive slots. The negative side always reaches exactly one further than the positive side:
| Width | Typical name | Minimum | Maximum | Patterns |
|---|---|---|---|---|
| 4-bit | nibble | −8 | 7 | 16 |
| 8-bit | byte, sbyte, int8 | −128 | 127 | 256 |
| 16-bit | short, int16 | −32,768 | 32,767 | 65,536 |
| 32-bit | int, int32 | −2,147,483,648 | 2,147,483,647 | 4,294,967,296 |
| 64-bit | long, int64 | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 18,446,744,073,709,551,616 |
The calculator above covers 4 through 32 bits. The 64-bit row is listed for reference because it is the width most database and language integer columns default to today.
Common byte values side by side
These are the patterns that show up constantly in memory dumps, protocol traces and debugger output. Every row is one byte read three ways:
| Signed | Binary | Hex | Unsigned |
|---|---|---|---|
| 127 | 0111 1111 | 0x7F | 127 |
| 100 | 0110 0100 | 0x64 | 100 |
| 42 | 0010 1010 | 0x2A | 42 |
| 1 | 0000 0001 | 0x01 | 1 |
| 0 | 0000 0000 | 0x00 | 0 |
| −1 | 1111 1111 | 0xFF | 255 |
| −2 | 1111 1110 | 0xFE | 254 |
| −37 | 1101 1011 | 0xDB | 219 |
| −56 | 1100 1000 | 0xC8 | 200 |
| −100 | 1001 1100 | 0x9C | 156 |
| −128 | 1000 0000 | 0x80 | 128 |
Read down the last two columns and the rule becomes visible: for any negative signed value, unsigned = signed + 256 in a byte. The pattern 0xC8 is 200 to a network parser and −56 to a signed temperature reading, and nothing in the byte itself settles the argument.
Subtraction becomes addition
The reason hardware designers settled on two's complement is that it removes the special case for negative operands. To compute 5 − 9 in a byte, the processor forms the two's complement of 9 (00001001 inverted is 11110110, plus 1 is 11110111, which is −9 and 0xF7) and simply adds: 00000101 + 11110111 = 11111100. The sign bit is 1, so read it back by inverting and adding one: 00000011 + 1 = 00000100 = 4, therefore the value is −4. Correct, with one adder and no branch on the sign.
Overflow: when the answer will not fit
Because the width is fixed, some correct answers have nowhere to go. Add 100 and 50 in a byte: the true sum is 150, but the signed ceiling is 127. The bit-level addition 01100100 + 00110010 gives 10010110, and that pattern reads back as −106, because 150 − 256 = −106. Two positive inputs produced a negative output, which is exactly the hardware test for signed overflow: overflow occurred if the operands share a sign and the result carries the opposite one.
The mirror case is just as common. Adding −100 and −50 in a byte gives a true sum of −150, below the −128 floor, and the stored pattern 01101010 reads back as +106. Overflow is not an error the machine raises for you in most languages; it is a silent wrap. In C and C++ signed overflow is undefined behavior, so the compiler is free to assume it never happens, which is how optimizers occasionally delete an overflow check that a programmer wrote in good faith.
Sign extension when a value gets wider
Copying a signed byte into a 16-bit or 32-bit slot requires the new high bits to be filled with copies of the sign bit, not with zeros. The byte 11011011 (−37) becomes 1111111111011011 in 16 bits, which is 0xFFDB and still −37. Zero-filling instead would produce 0000000011011011, which is 219, a completely different number. This is the mechanism behind a whole family of bugs where a temperature, an offset or an audio sample turns into a large positive value the moment it is widened by the wrong instruction.
How to use this calculator
- Pick the bit width that matches the register or data type you care about: 4 for classroom exercises, 8 for a byte, 16 for a short, 32 for a typical int. The signed range for the chosen width is printed underneath.
- Choose the direction. "Decimal to binary" takes a signed number such as −37 and produces the stored pattern. "Binary to decimal" takes a pattern such as 11011011 and reports what it means as a signed value.
- Type the value. Decimal accepts an optional minus sign; binary accepts 0s and 1s, with or without spaces, and is padded on the left to the full width.
- Read the result card for all three readings at once: signed decimal, unsigned decimal and hexadecimal, plus the grouped bit pattern.
- Follow the steps panel to see the magnitude, the inverted bits and the add-one result, which is what most homework and exams want you to show.
- Test an addition in the bottom panel to see whether a sum stays inside the range or wraps, and by how much.
Who this calculator is for
- Computer science students working through a digital logic or computer architecture assignment that asks for the invert-and-add-one steps, not just the answer.
- Embedded and firmware developers decoding sensor registers, where a signed reading arrives as two bytes and the sign has to be reconstructed by hand.
- Reverse engineers and debuggers staring at a hex dump and trying to decide whether 0xFFDB is a small negative number or a large unsigned one.
- Protocol implementers checking that a field declared as int16 in a spec round-trips correctly through their encoder.
- Anyone chasing an overflow bug who needs to confirm that a mysterious −106 really is the wrapped form of 150.
Key terms
- Bit width: the number of columns in the register. It defines the range, the wrap point and the weight of the sign column.
- Sign bit (MSB): the leftmost bit. 1 means negative, 0 means zero or positive. It is a real place value, not a separate flag.
- One's complement: the intermediate result after flipping every bit, before the add-one step. It is also a historical signed format with two zeros.
- Wrap-around (modular arithmetic): the behavior that makes 127 + 1 land on −128 in a byte. The values form a circle, not a line.
- Sign extension: filling the new high bits with copies of the sign bit when a value is widened, so the number keeps its meaning.
- Signed vs unsigned: two interpretations of one pattern. Unsigned treats every column as positive; signed makes the leading column negative.
- Overflow: a result whose true value lies outside the representable range, so the stored answer is wrong by exactly one full span.
What changes the result
Only three things move the answer, and it is worth being deliberate about each:
- The bit width. The same decimal value produces a different pattern at every width, and the same pattern means different things depending on how wide the register is assumed to be. 1011 is −5 in four bits and +11 in eight.
- The sign convention. Reading a pattern as unsigned rather than two's complement shifts every negative value up by the full span, turning −1 into 255 in a byte.
- Whether the value fits. Anything outside the range silently wraps by a multiple of the span, so a range check has to happen before the conversion, not after.
Tips and quick checks
- Verify by adding. A pattern and its magnitude must add to all zeros inside the width. If they do not, a step went wrong.
- Use the shortcut for round numbers. The two's complement of a power of two is easy: −64 in a byte is 0xC0, −32 is 0xE0, −16 is 0xF0. Each halving adds another leading one.
- Copy from the right. A faster hand method than invert-and-add-one: copy bits from the right up to and including the first 1, then invert everything to the left of it. It gives the same pattern in one pass.
- Group bits in fours. Writing 1101 1011 instead of 11011011 makes the hex digits fall out immediately, since each group of four is one hex digit.
- Watch the extremes. The minimum value is its own negative: negating −128 in a byte gives −128 again, because +128 has no pattern.
Limitations and assumptions
- The tool covers integers only. Floating-point values use IEEE 754, which stores a sign bit, a biased exponent and a fraction, and does not use two's complement at all.
- Widths are limited to 4, 8, 16 and 32 bits. The same rules extend to 64 bits and beyond, but 64-bit values exceed the exact-integer range of standard browser arithmetic.
- It shows a single value in isolation, with no byte order. Whether a multi-byte value appears as DB FF or FF DB in memory depends on the platform's endianness.
- It models two's complement only, not sign-magnitude, one's complement, excess-K or binary-coded decimal, all of which appear in older or specialized hardware.
- Language rules differ. Java, C#, Go and Rust all define signed integers as two's complement with specified wrapping or trapping behavior, while C and C++ leave signed overflow undefined, so identical arithmetic can behave differently at the source level.
Which calculator to use when
If your numbers are never negative and you just need base conversion or plain binary arithmetic, the Binary Calculator is the simpler tool. If you are working from a hex dump and want hexadecimal arithmetic or a hex-to-decimal conversion without any sign question, use the Hex Calculator. When powers of two themselves are the question, such as how big 2 to the 31st is, the Exponent Calculator answers it directly. Come back to this page whenever a minus sign, a fixed register width or a suspicious wrap-around is involved.
Sources
Two's complement is a mathematical definition rather than a published rate or a policy figure, so no external source is required for the numbers on this page. Every pattern, range and worked example here follows directly from two exact statements: a value is stored as its residue modulo 2 raised to the bit width, and the leading column carries the weight −2 raised to (width − 1). The 4-bit table, the byte samples, the range table and the overflow examples were all computed from that definition and can be reproduced by hand with the invert-and-add-one procedure described above. The bit-width names used in the tables (byte, short, int, long) follow the fixed-width integer conventions of the ISO C standard's stdint types, which specify int8, int16, int32 and int64 as exactly-sized two's complement integers.
โ ๏ธ Common mistakes & edge cases
Forgetting to pad to the full width first
Inverting 100101 instead of 00100101 gives 011010 rather than 11011010, and the sign comes out wrong. Always write the magnitude in the complete register width before you flip a single bit.
Inverting without adding one
Stopping at the inverted pattern gives one's complement, which is off by exactly 1 and has two representations of zero. In a byte, 11011010 is −37 only after the add-one step turns it into 11011011.
Reading a signed byte as unsigned
0xFF is −1 as a signed byte and 255 as an unsigned one. Sensor drivers and network parsers that pick the wrong type turn small negative readings into values just under the maximum, which looks like a sensor fault but is a type bug.
Assuming +128 exists in a byte
The signed byte range stops at 127. Writing 128 into it stores 10000000, which reads back as −128, and negating −128 returns −128 because its positive twin has no pattern.
Zero-extending instead of sign-extending
Widening the byte 11011011 to 16 bits must give 1111111111011011 (−37). Filling the new bits with zeros gives 219, and the error survives every later calculation without a warning.
Trusting an overflow check written after the fact
Testing whether a sum came out negative after the addition is unreliable in C and C++, where signed overflow is undefined and the compiler may remove the test. Check the operands against the range before adding, or use a wider type.
❓ Frequently asked questions
How do you calculate two's complement?
Take the positive magnitude, write it in binary padded to the chosen bit width, invert every bit (change each 0 to 1 and each 1 to 0), then add 1. For example, in 8 bits, 37 is 00100101, inverting gives 11011010, and adding 1 gives 11011011, which is -37 in two's complement. Positive numbers and zero need no conversion at all: they are stored as plain binary.
What is 11011011 in two's complement?
In 8-bit two's complement, 11011011 is -37. The leftmost bit is 1, so the value is negative, and the sign column carries a weight of -128: -128 + 64 + 16 + 8 + 2 + 1 = -37. Read as an unsigned byte the same pattern is 219, and in hexadecimal it is 0xDB. The bit pattern alone never tells you which reading is intended; the type does.
Why is -1 all ones in two's complement?
Because adding 1 to a pattern of all ones carries out of every column and leaves zero inside the register. In 8 bits, 11111111 + 00000001 = 100000000, and the ninth bit falls off the end, so the stored result is 00000000. Anything that adds to zero is by definition -1, so 11111111 must be -1. The same holds at every width: 0xFFFF is -1 in 16 bits and 0xFFFFFFFF is -1 in 32 bits.
What is the range of an 8-bit two's complement number?
An 8-bit signed integer covers -128 to 127, which is 256 distinct values. The negative side reaches one further than the positive side because the all-zero pattern is used for zero, leaving 128 patterns for negatives and only 127 for positives. In general an n-bit signed integer runs from -2^(n-1) to 2^(n-1) - 1.
Why is there no positive 128 in a signed byte?
Because 8 bits hold 256 patterns and one of them has to represent zero. Splitting the rest evenly gives 128 negative values (-128 through -1) and 127 positive values (1 through 127). The pattern 10000000 is claimed by -128, so there is no room for +128. The practical consequence is that negating -128 in a byte overflows and returns -128 again.
How does two's complement handle subtraction?
It turns subtraction into addition. To compute A - B the processor forms the two's complement of B and adds it: in 8 bits, 5 - 9 becomes 00000101 + 11110111 = 11111100, which is -4. This is the main reason two's complement won over other signed formats: one adder circuit handles both addition and subtraction, with no special case for signs.
What is the difference between one's complement and two's complement?
One's complement flips every bit and stops there, which produces two different patterns for zero (all zeros and all ones) and requires an end-around carry when adding. Two's complement flips every bit and then adds 1, giving a single zero, a range that is one wider on the negative side, and addition that works without correction. Modern CPUs use two's complement; one's complement survives mainly as the intermediate step and in checksum algorithms.
How do I know if a two's complement number is negative?
Look at the most significant bit, the leftmost one in the register. If it is 1 the value is negative; if it is 0 the value is zero or positive. That single bit is called the sign bit. Be careful to count to the full width first: the four-bit pattern 1011 is -5, but the same digits stored in a byte as 00001011 are +11.
What is signed integer overflow?
Overflow happens when the true mathematical result falls outside the range the bit width can hold, so the stored value wraps around. In 8 bits, 100 + 50 = 150, but 150 is above the 127 ceiling, so the stored pattern 10010110 reads back as -106. The hardware signal is that both operands share a sign while the result carries the opposite sign. In C and C++ signed overflow is undefined behavior, which is why widening the type is the safe fix.
How do I convert two's complement back to decimal?
If the sign bit is 0, read the pattern as ordinary binary. If it is 1, either apply the weighted place values with the leading column negative (-128 for a byte), or invert the bits, add 1, read that as a positive number, and put a minus sign in front. Both routes give the same answer; the calculator above shows the weighted breakdown for every bit you set.
What is sign extension?
Sign extension widens a signed value without changing it by copying the sign bit into all the new high bits. The byte 11011011 (-37) becomes 1111111111011011 in 16 bits, still -37, and 0xFFDB in hex. Positive values are extended with zeros instead. Getting this wrong is a classic bug: zero-extending -37 from a byte would give 219 instead of -37.
Is 0x80 equal to 128 or -128?
Both readings exist, and the type decides. As an unsigned byte 0x80 is 128; as a signed byte in two's complement it is -128. The bits are identical either way, which is why a value read into the wrong type flips sign without any warning. Switch the bit width in the calculator and the same hex digits take on a different signed value again.
Do floating-point numbers use two's complement?
No. Floats follow the IEEE 754 format, which stores a separate sign bit, an exponent field with a bias, and a fraction, so negation is a simple sign-bit flip rather than an invert-and-add-1. Two's complement applies to integer types such as byte, short, int, and long. This calculator covers the integer case only.
Is this two's complement calculator free?
Yes. It is completely free, needs no sign-up, and runs entirely in your browser, so nothing you type is sent anywhere. Convert as many values as you like at 4, 8, 16, or 32 bits, in either direction, and use the addition panel to test overflow cases.
๐ก Good to know
There is only one zero
Sign-magnitude and one's complement both produce a positive zero and a negative zero, which forces extra hardware and extra comparisons. Two's complement has exactly one zero pattern, all bits clear, which is a large part of why it became universal.
The values wrap in a circle
Counting up from 127 in a byte does not stop; it lands on −128 and keeps going. Thinking of the range as a dial rather than a line makes both overflow and the missing +128 obvious at a glance.
Hex is just four bits at a time
Because 16 is 2 to the fourth, each hex digit maps to exactly four bits. Grouping 1101 1011 gives D and B immediately, so 0xDB. A long negative value showing many leading F digits is the fastest visual clue that you are looking at a small negative number.
Related Calculators
Density Calculator
Solve density, mass or volume with rho = mass / volume
Pythagorean Theorem Calculator
Solve for any side of a right triangle
Projectile Motion Calculator
Range, max height and flight time of a projectile
Synthetic Division Calculator
Divide a polynomial by (x - c) with synthetic division