Two’s complement calculator
Two’s complement holds a negative value in a fixed number of bits.
To get the bits, invert every bit of the magnitude and add 1.
The value -6 in 8 bits gives 11111010, which reads as 250 without a sign.
Try a two’s complement conversion in the bit editor
The bits 11111010, which give -6. Edit the expression or select a bit to flip it.
7
6
5
4
3
2
1
0
-6
value · 8 bits · signed
- hex
- 0xFA
- binary
- 0b11111010
Read the bits as
Bit width
A two’s complement conversion step by step
Read the bits in the editor as a value: The steps follow the value above.
- Write the bits: 11111010
- The top bit is 1, so the value is negative.
- Invert every bit: 00000101
- Add 1: 00000110, which is 6.
- The signed value is -6.
A few values in 8-bit two’s complement
| Signed value | Bits | Unsigned reading | Hex |
|---|---|---|---|
| -1 | 11111111 | 255 | 0xFF |
| -6 | 11111010 | 250 | 0xFA |
| -128 | 10000000 | 128 | 0x80 |
| 0 | 00000000 | 0 | 0x00 |
| 127 | 01111111 | 127 | 0x7F |
Where a two’s complement conversion is used
A sensor that reports a temperature below zero sends these bits.
A register field that holds an offset needs the same form.
A value that reads as a large positive number is often a negative value with the wrong sign mode.
A two’s complement conversion: points to note
- A change of the sign mode does not change the bits. The same bits give a different reading.
- The top bit is not a minus sign. The top bit carries part of the value.
- A wider bit width needs the sign copied into every new bit, which the name sign extension covers.
- The range is not symmetric, because zero takes one of the positive codes.
The full bit editor
The full editor holds many values, evaluates expressions across them, and reads live values from a serial port.
Open this value in the full editorQuestions about a two’s complement conversion
- What is -6 in 8-bit two’s complement?
- The bits are 11111010. The same bits read as 250 without a sign.
- How do I convert the bits back to a decimal value?
- Invert every bit, add 1, then read the result as a magnitude with a minus sign in front.
- What is -1 in any width?
- Every bit has the value 1. In 8 bits that is 0xFF, and in 32 bits it is 0xFFFFFFFF.