Subtracting binary is another fundamental operation that is used to subtract one binary number from another.

Just like in decimal subtraction, the corresponding bits of the numbers are subtracted from right to left, taking into account the borrows.

Binary subtraction can result in a negative result if the number to be subtracted is greater than the number it is being subtracted from.

### Borrow

“Borrow” occurs when a number cannot be subtracted from another due to its value. This happens when the number being subtracted is greater than the number it is being subtracted from.

In binary, this only happens when we try to subtract a `1`

from a `0`

. In this case, the result is ‘1’, and a value is “borrowed” from the next more significant bit.

### Example

For example, to subtract the binary number `1101`

(13 in decimal) from `1011`

(11 in decimal):

```
1011
- 1101
______
1110
```

The result is `1110`

, which is equal to -2 in decimal.

## Binary subtraction with two’s complement

Binary subtraction is not difficult, but it can be a bit of a pain. It is generally faster using the two’s complement method. This method allows us to convert subtraction into an addition, and we know how to quickly add two binary numbers “by hand”.

To subtract binary numbers using the two’s complement, we follow these steps:

- Convert the number to be subtracted into its two’s complement
- Add the number from which it is being subtracted to the two’s complement
- Discard any overflow bit at the end

We could also do a manual procedure to subtract by hand very quickly, as we did with addition. But in general **it is better to know two or three “tricks” to do things quickly**, than to try to learn 27 and then mix or forget them all.

### Example

Let’s subtract the binary number `1101`

(13 in decimal) from the binary number `0100`

(11 in decimal) using the two’s complement:

```
1101
- 0100
_____
¿?¿?
```

First, we convert `1011`

into its two’s complement. To do this, we invert all the bits and then add 1:

```
1011 (Original number) => Two's complement
0100 (Inverted bits)
+ 1 (Add 1)
_____
0101 (Two's complement of 1011)
```

We add

`1101`

to the two’s complement of`1011`

:`1101 (Original number) + 0101 (Two's complement of 1011) ______ 1 0010 (Result)`

By discarding the overflow bit on the left, we get the final result: `0010`

, which is equal to 2 in decimal.

The advantages of using the two’s complement for subtractions are that it simplifies the process and eliminates the need to handle borrows.

## Bonus: How to quickly subtract 1 in binary

Special bonus, how to quickly subtract 1 from a binary number. A very simple operation, but since it is very common, it is convenient to know how to do it.

Fortunately, subtracting 1 in binary is “a piece of cake”. Simply

- Go through the binary number from the right
- Look for the first
`1`

you find, and change it to`0`

- From that position, all
`1`

s to the right.

For example, if we had to subtract 1 from `1000100000`

We look for the first `1`

We change it to `0`

and, from there to the right, all `1`

s.

So the result is `1000011111`

. As you can see, adding one is very quick.