Two’s complement converter
Convert a negative or positive integer to its 8, 16 or 32-bit two’s complement bits, or a hex or binary pattern to the signed value.
The smallest signed register that holds -42 is 7-bit.
What it means
Where does it sit?
A dash means the value does not fit that width: an 8-bit register holds −128 to 127.
Every unit
That integer in every register
Learn
Three things worth knowing
-
Flip and add one
To negate: invert every bit, then add 1. 5 is 0000 0101; −5 is 1111 1010 + 1 = 1111 1011.
-
Why not a sign bit
Early machines used a sign bit or ones’ complement and had two zeros (+0 and −0). Two’s complement has one zero and lets the same adder handle signed and unsigned numbers.
-
The range is lopsided
A signed byte goes from −128 to +127: there is one more negative number. Negating −128 overflows back to −128.
Shortcuts
Popular integer conversions
Background
About two’s complement
Almost every processor stores signed integers in two’s complement. A negative number −n is stored as 2bits − n, so in a byte −1 is 255 (1111 1111) and −128 is 128 (1000 0000). The top bit tells the sign.
The trick makes addition work without special cases: adding 1 to 1111 1111 overflows to 0000 0000, which is exactly −1 + 1.