Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python question Write a function add_bytes (b1, b2) that takes as inputs two strings b1 and b2 that represent bytes (i.e., 8-bit binary numbers). The

image text in transcribed

python question

Write a function add_bytes (b1, b2) that takes as inputs two strings b1 and b2 that represent bytes (i.e., 8-bit binary numbers). The function should compute the sum of the two bytes and return that sum in the form of a string that represents an 8-bit binary number. For example: >>> add_bytes('00000011', '00000001') '00000100' >>> add_bytes('00011100', '00011110') '00111010' Note that you will need to ensure that the result is exactly 8 bits long. If the result is too short, you should add Os on the left-hand side to get a string that is 8 characters long. If the result is too big to be represented in 8 bits, you should return the rightmost 8 bits of the result. For example: >>> add_bytes('10000011', '11000001') # the result (101000100) has 9 bi '01000100' Here again, you should not use recursion or perform any binary arithmetic. Rather, you should make use of the conversion functions that you wrote for problem 1. Hint: To ensure that your result has the correct length, we encourage you to do the following: - Use your conversion functions to determine the binary representation of the sum, and store that result in a variable. - Determine the length of the result using the len() function, and store that length in a variable. Then you can use conditional execution (if-else or if-elif-else) to decide what you should return. - If the result is less than 8 bits long, precede the result with the correct number of leading '0's. You can use the multiplication operator to produce the necessary string of '0's. Make sure that you also handle the case in which the result is more than 8 bits long

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

Sql++ For Sql Users A Tutorial

Authors: Don Chamberlin

1st Edition

0692184503, 978-0692184509

More Books

Students also viewed these Databases questions