# Binary Addition and Subtraction

Here, we are going to learn about the **Binary addition and subtraction**: **what are the binary addition and subtraction**, and how to perform them?

Submitted by Saurabh Gupta, on October 20, 2019

## 1) Binary Addition

Since binary numbers consist of only two digits 0 and 1, so their addition is different from decimal addition. **Addition of binary numbers** can be done following certain rules:

A | B | Sum | Carry |
---|---|---|---|

0 | 0 | 0 | 0 |

0 | 1 | 1 | 0 |

1 | 0 | 1 | 0 |

1 | 1 | 0 | 1 |

The above table contains two bits a and b, their sum and carry.

**On adding,**

0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, 1 + 1 = 10 (i.e., sum is 0 and carry is 1)

Let's do some exercise and solution some questions based on binary addition to get more of the topic.

**Example 1: Perform (10) _{2} + (11)_{2}**

**Solution:**

Using the rules provided above, sum operation can be performed as:

Therefore, **(10) _{2} + (11)_{2} = (101)_{2}**

**Verification:**

We can verify our result by converting the above binary numbers into decimal numbers and then verifying the sum.

Here, **(10) _{2} = (2)_{10}**,

**(11)**and

_{2}= (3)_{2}**(101)**, thus when we will add

_{2}= (5)_{10}**2**and

**3**we get sum as

**5**.

**Example 2: Perform (1) _{2} + (1)_{2} + (1)_{2} + (1)_{2}**

**Solution:**

Using the rules provided above, sum operation can be performed as:

**Example 3: Perform (110) _{2} + (111)_{2} + (101)_{2}**

**Solution:**

Using the rules provided above, sum operation can be performed as:

**Verification:**

We can verify our result as **(110) _{2}=(6)_{10}**,

**(111)**,

_{2}=(7)_{10}**(101)**and

_{2}= (5)_{10}**(10010)**. So when we will add

_{2}= (18)_{10}**6 + 7 + 5 =18**, which we are getting as our answer.

## 2) Binary Subtraction

The binary subtraction is performed like decimal subtraction, the rules for binary subtraction are:

A | B | Difference | Borrow |
---|---|---|---|

0 | 0 | 0 | 0 |

0 | 1 | 1 | 1 |

1 | 0 | 1 | 0 |

1 | 1 | 0 | 0 |

**Example 1: Subtract (10) _{2} from (1001)_{2}**

**Solution:**

In column C_{2}, 1 can't be subtracted from 0 so, we have to borrow 1 from column C_{3}, but C_{3} also has a 0, so 1 must be borrowed from column C_{4}, the 1 borrowed from column C_{4} becomes 10 in column C_{3}, now keeping 1 in column C_{3} bringing the remaining 1 to column C_{2} which becomes 10 in column C_{2} thus 10 – 1= 1 in column C_{2}.

In column C_{3}, 1 – 0 = 1

In column C_{4}, 1 after providing borrow 1 is reduced to 0.

Therefore, **(1001) _{2} – (10)_{2} = (111)_{2}**

**Example 2: Subtract (111.111) _{2} from (1010.01)_{2}**

**Solution:**

In Column C_{0}, 1 can't be subtracted from 0, so we have to borrow 1 from column C_{1}, which becomes 10 in column C_{0}, thus 10 – 1 = 1,

In column C_{1}, after providing borrow 1 to C_{0}, C_{1} is reduced to 0. Now 1 can't be subtracted from so borrow 1 from C_{2}, but it is also 0, so borrow 1 from C_{3} which is also 0, so borrow 1 from C_{4}, reducing column C_{4} to 0. Now, this 1 borrowed from column C_{4} becomes 10 in column C_{3}, keep 1 in the column C_{3} and bring other 1 to column C_{2}, which makes column C_{2} as 10 now again bring 1 from C_{2} to C_{1}, which reduces C_{2} to 1 and makes C_{1} as 10.

Thus, In Column C_{1}, 10 – 1 = 1

In Column C_{2}, 1 – 1 = 0

In Column C_{3}, 1 – 1 = 0

In Column C_{4}, we now have 1 to be subtracted from 0 which is not possible so we will borrow 1 from Column C_{5}, but Column C_{5} has a 0 so borrow 1 from C_{6} making C_{6} to be 0 and bring it to C_{5} which makes it 10 in C_{5}, keep 1 in C_{5} and bring the other 1 to C_{4} which makes C_{4} as 10 thus

In column C_{4}, 10 – 1 = 1

In column C_{5}, 1 – 1 = 0

In column C_{6}, 0 – 0 = 0

Hence, the result is **(1010.01) _{2} – (111.111)_{2} = (0010.011)_{2}**

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.