Question: Addition is a common arithmetic operation, and it can be performed on numbers in any base. In fact, binary (base 2) addition is simpler than

Addition is a common arithmetic operation, and it can be performed on numbers in any base. In fact, binary (base 2) addition is simpler than base 10 addition because there are fewer rules. Like base 10 addition, binary addition works from right-to-left, adding one column of digits at a time, according to the following rules:

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 0, plus a carry bit of 1 for the next column (1 + 1 = 2; 2 is 10 in binary)

If you have three 1s to add (1 + 1, plus a carry bit of 1), the sum is 1, with a carry bit of 1 (1 + 1 + 1 = 3; 3 is 11 in binary). For example, consider the following addition problem (eleven + eleven, in base 10; eleven is 1011 in binary/base 2): 1 1 1 1011 1011 1011 1011 1011 1011 +1011 +1011 +1011 +1011 +1011 +1011 ----- ----- ----- ----- ----- ----0 10 110 0110 10110 1. We start by adding the rightmost column. 1 plus 1 gives us 0, plus a carry bit of 1. 2. The second column has two 1s, plus a carry bit of 1. 1 + 1 + 1 equals 1, plus another carry bit of 1. 3. The third column (counting from the right) has a 1 (the carry bit) plus two 0s. This adds up to 1, with no carry value. 4. Finally, the fourth (leftmost) column has two 1s. 1 plus 1 equals 0, with a carry bit of 1. 5. There is no column left to absorb the carry bit, so the carry bit "falls down" and becomes the fth (leftmost) bit, to give us a nal answer of 10110 (which is 22 in base 10). Complete the addBinary() function, which takes two strings of equal length consisting entirely of 0s and 1s. The function uses the following process to return a new string corresponding to the sum of its (binary) arguments. The basic algorithm is as follows: 1. Set the result to the empty string 2. Set the carry value to 0 3. For each index position, from the last/rightmost index down through index 0: (a) Add the (integer) value at the current index from each of the operands (b) Add the carry value (if any) to the sum from the previous step, then reset the carry value to 0 (c) Examine the current sum: If the sum is less than 2, prepend it to (add it to the beginning of) the result Otherwise, if the sum is 2, set the carry value to 1 and prepend 0 to the result Otherwise, if the sum is 3, set carry to 1 and prepend 1 to the result 4. If the carry value is 1 after the loop ends, prepend 1 to the result one nal time Note that the return value will always be the same length as (or one bit longer than) the arguments.

Function: addBinary("1011", "1011") RETURN VALUE-10110

Function: addBinary("011", "111") RETURN VALUE- 1010

Function: addBinary("11010", "11101") RETURN VALUE- 110111

the problem begins like this:

def addBinary (first,second):

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!