Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using python3 to solve the problem Addition is a common arithmetic operation, and it can be performed on numbers in anyba In fact, binary (base

image text in transcribedimage text in transcribed

Using python3 to solve the problem

Addition is a common arithmetic operation, and it can be performed on numbers in anyba In fact, binary (base 2) addition is simpler than base 10 addition becusethere are fower rules. Like base 10 addition, binary addition works from right-to-left, adding one column of digits at a time, accoding to the following rule 0400 041-1 140- 11-0, plus a carry bit of for the next column -2;2 10 in binary) If you have three ls to add (1 1, pus a carry bit of 1), the sum is1, with a carry bit of 1-3:3 is 1 in binary) For example, consider the following addition problem (eleven + eleven, in base10; eleven is 1011 in binary/base 2): 1011 1011 1011 1011 1011 10111011 1011 1011 1011 1011 1011 110 0110 10110 1. We start by adding the rightmost colum1 plus 1 gives0, plus a carry bit of 1 2. The second column has two 18, plus a carry bit o 1. 1+1+1 quas1, plus another carry bit of 1 3. The third column (counting from the right) has a 1 (the carry bit) plus two Os. This adds up to1, with no carry value. Finally, the fourth (leftot) column has two 1. 1 plus 1 equals 0, with a enrry bit of 5. There is no column left to abeorb the carry bit, so the carry bit falls down' and becomes the fifth (lbi, to give us a final answer of 10110 (which is 22 in base 10) Complete the addBinar fuction, which takes two strings of equal length conisting entirely ofOs 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 poesition, from the lnst/rightmost index down through index 0: (a) Add the (integer) value at the current index from each of the operands (b) Add the earry value (if any) to the sum from the previous step, then ree 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 sumi 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'to the result 1. If the carry value is 1 after the loop ends, prepend' to the reult one final time Note that the return value will always be the same length as (or one bit longer than) the arguments Examples: inction Ca addBinary( 1011", "1011" addBinary("011" addBinary 11010", 11101" oturn Value 0110 1010 11011

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

2. Show the trainees how to do it without saying anything.

Answered: 1 week ago