Two's complement is a representation of signed integers in binary code.
An N-bit two's complement code can represent the range of integers from -(2^{N-1}) to 2^{N-1}-1.
The negation of the two's complement number `n`

is `not(n)+1`

.
The following table shows how a 4-bit binary code represents unsigned integers and two's complement signed integers.

Binary code | Unsigned integer | Signed integer |
---|---|---|

`0000` |
`0` |
`0` |

`0001` |
`1` |
`1` |

`0010` |
`2` |
`2` |

`0011` |
`3` |
`3` |

`0100` |
`4` |
`4` |

`0101` |
`5` |
`5` |

`0110` |
`6` |
`6` |

`0111` |
`7` |
`7` |

`1000` |
`8` |
`-8` |

`1001` |
`9` |
`-7` |

`1010` |
`10` |
`-6` |

`1011` |
`11` |
`-5` |

`1100` |
`12` |
`-4` |

`1101` |
`13` |
`-3` |

`1110` |
`14` |
`-2` |

`1111` |
`15` |
`-1` |

Copyright © 2014 Barry Watson. All rights reserved.