Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**** In python, must use recursion, no loops allowed**** The two's-complement representation of a non-negative integer is simply its standard representation in base 2. However,

**** In python, must use recursion, no loops allowed****

The two's-complement representation of a non-negative integer is simply its standard representation in base 2. However, the two's-complement representation of a negative integer is obtained by first finding the base-2 representation of its absolute value, then flipping each bit, and finally adding the number 1.

This problem has two parts.

First, write a function called TcToNum that takes as input a string of 8 bits representing an integer in two's-complement, and returns the corresponding integer. Notice here that since the input string is always exactly 8 bits long, we will often have leading 0s in the input, as in "00000001". Here is sample input and output:

>>> TcToNum("00000001") 1 >>> TcToNum("11111111") -1 
>>> TcToNum("10000000") -128 >>> TcToNum("01000000") 64 

Next, write a function called NumToTc that takes as input an integer N, and returns a string representing the two's-complement representation of that integer. You should assume that exactly 8 bits are being used, so only a certain range of numbers can be represented! The output should be exactly 8 bits long in all cases. If the input N is such that it cannot be represented in two's-complement with 8 bits, the function should return "Error". Here is some sample input and output:

>>> NumToTc(1) '00000001' >>> NumToTc(-128) '10000000'
>>> NumToTc(200) 'Error' 

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

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions