Binary Addition


Youtube


#How to add binary numbers?

All 5 possible scenarios of binary addition:


You add binary numbers the same as you would regular decimal numbers! In a decimal system, we carry to the left whenever we go above 9. In a binary system we carry when we go above 2 as highlighted in red.

To make sense of why 1 + 1 + 1 = 11, imagine 5 + 5 + 5. We add the two fives: 5 + 5 = 10 and we have to carry over the 1 but we still have a 5 left over, leaving us with 15.

Let’s add 4 bit binary numbers 0111 + 1110.

  1. Start from right to left (<–)
      0 1 1 1 (7)
    + 1 1 1 0 (14)
    __________
            1
            
  2. Add normally and carry as needed
        1 <- carry 
      0 1 1 1 (7)
    + 1 1 1 0 (14)
    __________
          0 1 
      1 1 <- carry 
      0 1 1 1 (7)
    + 1 1 1 0 (14)
    __________
        1 0 1  
    1 1 1 <- carry 
      0 1 1 1 (7)
    + 1 1 1 0 (14)
    __________
    1 0 1 0 1 (21) 
  3. Consider effect of overflow

     1 1 1 <- carries 
      [0 1 1 1] (7)
    + [1 1 1 0] (14)
    _______
     1[0 1 0 1] (21) or (5 if we can’t expand)
            

    The answer can be either 0101 or 10101 on paper! In software however it depends on the programming language. Some decide to crash(rust), some end up wrapping around (C)

    Recall that computers have finite limits to what they can represent as noted by the brackets [] indicating a fixed width. 4 bits can only represent a maximum number of 16 things, and here, the sum of the two numbers go over 15! The overflow bit may be dropped if we can’t expand the bit-width, resulting in 5 as our answer. This property will be important later.

unsigned
DecimalBinary
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001
101010
111011
121100
131101
141110
151111

Check your understanding!

What is 101 + 001 ? (given a 3 bit-width)
    1
  1 0 1 (5)
+ 0 0 1 (1)
________
  1 1 0 (6)


What is 111 + 111 ? (given a 3 bit-width)
1 1 1
  1 1 1 (7)
+ 1 1 1 (7)
________
1 1 1 0 (14) or (6 because of overflow wraparound)


More Binary Addition problems with variable bit-width Here