# Signed Numbers: Two’s Complement

• In mathematics, negative numbers in any base are represented by prefixing them with a minus sign (“−”). However, in computer hardware, numbers are represented only as sequences of bits, without extra symbols.
• Two’s complement is one of the method to represent signed numbers using  binary numeral system, which uses only two symbols: typically “0” (zero) and “1” (one).
• Invert and add one: negating a number (whether negative or positive) is done by inverting or complementing all the bits (switched from “1” to “0” or “0 “to “1”) and then adding one to that result. See this post for why inversion and adding one works
• -x → invert(x) + 1
• OR Invert the result of the number minus one
• -x → invert(x-1)

#### Examples

binary number in double quote

``````-1 → invert(1) + "1" → invert("00000001") + "1" → "11111110" + "1" → "11111111"
OR
-1 → invert(1-1) → invert(0) → invert("00000000") → "11111111"

-8 → invert(8) + "1" → invert("00001000") + "1" → "11110111" + "1" → "11111000"
OR
-8 → invert(8-1) → invert(7) → invert("00000111") → "11111000"

-128 → invert(128) + "1" → invert("1000000") + "1" → "01111111" + "1" → "10000000"
OR
-128 → invert(128-1) → invert(127) → invert("01111111") → "10000000"``````