# 2's Complement

##### Written in Computer Science, General CS

2's Complement is a way of representing negative numbers. It works by

- Switching the role of 1 and 0 so
- Using the leftmost bit as a sign flag, i.e. if the leftmost bit is 1, the number is negative, if it is 0, the number is positive.

## EXAMPLES

`0000`

- zero`0001`

- one`0010`

- two`0011`

- three`0100`

to`0111`

- four to seven

`1111`

- negative one`1110`

- negative two`1101`

- negative three`1100`

to`1000`

- negative four to negative eight

By using the leftmost bit as a sign flag, the range of possible values goes down.

Also, you'll notice that negative numbers start at -1 (add -1 to the value), so that 1101 looks like it should be -2 but it is actually -2 + -1.

## Converting to 2s complement

Convert 0100 to negative 4 in 2s complement

- Invert all the bits

0100 -> 1011 - Add 1 to it (-4 + positive 1 = -3)

1011 -> 1100 - Done

1100 looks like -3 but don't forget that negative numbers in 2s compliment have an extra -1