Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given the code below: define the three functions decimalToBinary, binaryAddition, and binaryToDecimal so that the program will be able to add binary numbers. The description
Given the code below: define the three functions decimalToBinary, binaryAddition, and binaryToDecimal so that the program will be able to add binary numbers. The description of each function is as follows: def decimalToBinary (n) : - This function takes a positive integer n>0 as an argument and returns a string of integers where each character of the string is a binary digit, 0 or 1 obtained by the conversion of the integer argument n to binary. You are not allowed to use the built-in function bin( ). For example, decimalToBinary(3) "11" decimalToBinary (123) "1111011" decimalToBinary(2312) "100100001000" binaryAddition(str1, str2): - This function takes two string arguments str 1 and str 2 representing two binary numbers and returns a string of integers whose characters are obtained by the addition of the two binary numbers in binary addition format. - How to add two binary numbers? Binary addition is like decimal addition, except that it carries on a value of 2 instead of a value of 10. 0+0=01+0=10+1=11+1=10(thisis0carry1)1+1+1=11(thisis1carry1) To add two binary numbers, we simply add together the corresponding digits in each binary number from right to left and if the sum is greater than what fits in a single digit, we carry a 1 into the next column. For example, 11111110101+111011010010 We start by the first column from the right: 1) First column: 1+1=0 (with carry 1 ) 2) Second column: +0+1 (carried) =1 (no carry) 3) Third column: 1+1+ no carry =0 (carry 1) 4) Fourth column: 0+1+1 (carried) =0 (carry 1) 5) Fifth column: 1+1+1 (carried) =1 (carry 1 ) 6) Sixth column: 1+1 (carried) =0 (carry 1) 7) Seventh column: 1 (carried) binaryAddition("110101", "11101") "1010010" binaryAddition("110101", "11101") "1010010" def binaryToDecimal(binStr): - This function takes a string binStr representing a binary number and converts it to decimal and returns the decimal number. For example, binaryToDecimal("10") 2 binaryToDecimal("10100") 20 binaryToDecimal("101110110010") 2994 binaryToDecimal("1111101110110000") 64432
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started