Two’s Complement

First bit is 0 if non-negative, 1 if negative.

  • To negate a value:
    • Take the complement of all bits.
    • Add 1.

CS241 A1: Why did I have to subtract zero from my number to get two’s complement?

  • Faster way is to locate the rightmost 1 bit and flip all the bits to the left of it.

Decimal to Two’s Compliment

Compute -38_10

  1. Write 38 in binary: 38_10 = 00100110
  2. Take the complement of all the bits: 11011001
  3. Finally add 1: 11011010 This last value is -38_10.

Two’s Compliment to Decimal

Convert -38_10 = 11011010 to decimal:

  1. Flip the bits and add 1. The corresponding positive number is 38 and so the original number is -38.
  2. Treat the original number 11011010 as an unsigned number, convert to decimal and substract 2 from it (since we have 8 bits and the first bit is a 1 meaning it should be a negativ value.)
    1. 11011010 = 2^7 + 2^6 + 2^3 + 2^1 - 2^8 = 128 + 64 +16 +8 +2 - 256 = 218 -256 = -38