Two’s complement calculator
Encode a signed integer or decode a two’s complement bit pattern at a fixed width. Your inputs stay on your device.
Set up your task
Starting local processing
Ready. Processing stays in this browser.
2 Your result
Your result updates in this browser. Check your input if the result looks unexpected.
How to use Two’s complement calculator
Encode a signed integer or decode a two’s complement bit pattern at a fixed width. The pre-filled problem gives you a worked starting point. Compare the response with your own working before trying the self-check below.
- Check the example inputs: Direction, Signed decimal or bit pattern, Bit width.
- Change the inputs for your problem. Results update as you type, so finish entering the values before copying an answer.
- Use the working to check an intermediate value or assumption. Then change the example as suggested in the self-check and compare the result.
Supported inputs and limits
Choose 2 to 64 bits. A signed input must fit that width. To decode, enter exactly the listed number of bits.
How it works
Two’s complement gives a single bit pattern for zero and asymmetric signed limits. In eight bits, the range is −128 through 127. The very same bits can represent something else when read as unsigned. The width and the interpretation are part of the problem statement.
A signed value is encoded by taking its residue modulo 2 to the chosen width. Decoding treats the leading bit as a signal to subtract that same power of two from the unsigned value. Fixed-width arithmetic wraps around, which is a separate operation from validating a literal input.
Example
Run the pre-filled example. Its first result is “Binary: 11111011”.
- Direction
- Signed decimal to bits
- Signed decimal or bit pattern
- -5
- Bit width
- 8
Binary: 11111011 Signed range: -128 to 127 Encoded residue modulo 256: 251
Questions about Two’s complement calculator
How do I interpret a bit pattern with a leading zero?
In the chosen width, its signed value is the same as its unsigned value because the leading bit has no negative weight to apply.
Why do eight bits reject 128 as signed input?
The highest signed value is 127. The 10000000 pattern is the value −128 in that width.
Decode 11111111 as an eight-bit number.
Expect −1 signed and 255 unsigned. Widening −1 to sixteen bits requires extending the leading one, which leaves its signed value unchanged.
Related tools
Last updated