### Logical Negation

#### Buy or Subscribe

You can access this course in just a minute, and support my efforts to rid the world of crappy online courses!

This is where things get a bit weird. Binary has only two digits: a 1 and a 0, *nothing else*. So how do you represent a negative number? It turns out we have two ways of doing this.

## One's Complement

In decimal you sutract a number (let's say it's `y`

) from another number (let's say it's `x`

) by inverting it and then peforiming addition:

```
z = x + -y
```

Inverting a number in decimal is signified by using the `-`

sign operator. This equation is a bit noisy, however, so we tend to write it in the simpler form `x-y`

. We can do the exact same in binary, *sort of*, by inverting (or "flipping") a binary number to get it's complement.

Flipping is a straightforward thing: whenever you see a 1 you replace it with a 0 and vice-versa. That's what one's complement is: *inverting* and then signing a number. To "sign" a number in binary means setting the left-most bit to either a 0 (positive) or 1 (negative).

The one's complement of a binary 1 (`01`

), therefore, is `110`

. It looks a bit weird and takes some getting used to, but it works.

We can now perform subtraction. Let's try 1 - 1 in binary. Remember, the left-most digit is the sign:

```
001
110
---
111
```

Our answer is 111, which is the binary representation of zero. Which is weird because the binary representation of zero is also `000`

and, as it turns out, there are *two representations of zero* in one's complement. Which sucks and is confusing.

There's a mathematical reason why which is in the video.

## Two's Complement

We need to do a little more work to get everything line up the way we want, which is where *two's complement* comes in. It's the same as one's complement, but you just **add one** to get over that negative zero thing. Sounds kind of goofy, but it works:

```
001
110
001
---
000
```

You'll notice here that there's a carry as the last operation and that, if we're sticking to the rules, the answer should really be `1000`

. Why isn't it?

## No Last Carry in Two's Complement

The simple reason is that we've already accounted for the carry in two's complement by adding one. Nice and tidy. So: if there's a carry, you can just ignore it.