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).
  • Negative numbers are written with a leading one instead of a leading zero.
  • 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"

Eight-bit two’s complement

BINARY VALUETWO’S COMPLEMENT INTERPRETATIONUNSIGNED INTERPRETATION
0000 000000
0000 000111
0000 001022
0000 001133
0000 010044
0000 010155
0000 011066
0000 011177
0000 100088
0111 1110126126
0111 1111127127
1000 0000−128128
1000 0001−127129
1000 0010−126130
1111 0111-9247
1111 1000-8248
1111 1001-7249
1111 1010-6250
1111 1011-5251
1111 1100-4252
1111 1101-3253
1111 1110−2254
1111 1111−1255
Eight-bit two’s complement

Leave a Comment

Your email address will not be published.

nineteen − nine =