Binary

Signed Integers

One's Complement

Finding the one's complement form of a signed integer:

  1. Invert all the bits. (Apply the Bitwise NOT operator)
  2. Congrats you're done. It's that simple!

For Example:
0100101 -> 1011010

Two's Complement

Finding the two's complement form of a signed integer:

  1. Find the one's complement (invert all bits)
  2. Add 1 to the result from the last step. See: Binary Addition

For Example:
Lets find the binary representation of -7 in two's complement.

  • 7 in binary: 0111
  • Invert: 1000
  • Add 1: 1001
  • Compare to the table below

Another example, lets find -47:

  • 47 in binary: 00101111
  • Invert: 11010000
  • Add 1: 11010001

Two's complement conversion table:

Two's complement FormSigned Decimal
00000
00011
00102
00113
01004
01015
01106
01117
1000-8
1001-7
1010-6
1011-5
1100-4
1101-3
1110-2
1111-1

Key points:

  • The most significant bit (MSB) in a two's complement representation determines the sign: 0 for positive and 1 for negative.
  • Two's complement allows for efficient arithmetic operations like addition and subtraction on signed integers.
  • There's a unique representation for 0 (when all bits are 0).
  • The two's complement can only represent values with half the magnitude of unsigned numbers, because half of the values represent positive and the other half are negative (besides 0).

Signed Magnitude

The signed magnitude form represents negative numbers by using the MSB to denote sign. 0 for positive and 1 for negative.

For example:

DecimalSigned Magnitude
70111
-71111
4700101111
-4710101111

Key points:

  • Notice that positive numbers are exactly the same, but we cannot use the MSB to represent value, only sign.
  • There are two representations of 0, and (-0 and +0). This wastes information that we could use to represent more numbers.