Question
develop python program ***** STARTER CODE ***** def twosComplement(value, length): answer = '' # ADD YOUR CODE HERE return answer if __name__ == __main__: start
develop python program
***** STARTER CODE *****
def twosComplement(value, length): answer = '' # ADD YOUR CODE HERE
return answer
if __name__ == "__main__": start = int(input("Enter a base 10 value to convert: ")) b = int(input("Enter the new base as an integer between 1 and 16: "))
converted = changeBase(start, b) print(start, "(base 10) is", converted, "in base", b) print()
original = int(input("Enter a base 10 integer to convert to two's complement: ")) numBits = int(input("How many bits long should the output be? "))
tc = twosComplement(original, numBits) print(original, " (base 10) is ", tc, " in ", numBits, "-bit two's complement representation.", sep='') # removed inter-part spaces for formatting purposes print()
Test Cases for Part 2 (the twosComplement() function)
13 (base 10) is 00001101 in 8-bit two's complement
-108 (base 10) is 111110010100 in 12-bit two's complement
0 (base 10) is 00000 in 5-bit two's complement
-2 (base 10) is 11111110 in 8-bit two's complement
Show transcribed image text
2. Two's Complement (6 pointa) ii. Now we need to negate our binary atring value to make ita value negative again. The binary representation that we discussed in lecture a3sumes that we are only dealing with positive integera. In the real world, however, we need to handle negative integer values (e.g.. -5) as well. Start by inverting every bit; each 0' becomes 1, and each '1'becomes 0. One way to do this is to use a loop to build a new atring, one character at a time, based on the content of the original binary string. A more efficient solution uses the Python atring operation replace() (with a placeholder value) to swap 0s and 1a To get around this limitation, modern computers use a variation of binary called two's inverted binary.replace0 2hange old 0s to 2s inverted-inverted. replace ('1', '0') * replace old 1 with 0 inverted - inverted.replace 2. '1') change 2s baok to le Two's complement allowa us to represent both poaitive and negative numbers in binary, with the leftmoat bit serving as the sign bit (if the aign bit is 0, then the number is positive (or 0); if the sign bit is 1, then the number is negative). Two's complement iant as simple as simply aticking a sign bit at the beginning of a binary value, though; the presence and interpretation of the sign bit also affects many of the other bits in the number (meaning that the binary representations for +5 and -5 look very diferent from one another). For example, the 4-bit-long repreaentation of +5 in two's complement is 0101 (+5 in regular binary is 101;we add a leading 0 to emphasize that the value is poaitive). The 4-bit-long two's complement representation of -5 is 1011, however (the leading 1 indicatea that the value is negative, but notice that the last 3 bits have changed from 101 to 011) ii. The final step of the negation process is to add 1 to the inverted value using binary addition. Binary addition ian't terribly complicated, but we will use a (very) simplified equivalant to keep this assignment's length under control. The laat 0' in the number changes to 1', and every digit to ita right (which must be'1', by definition) changes to 0. Every digit before (to the left of) the last '0' stays the same. For example, "110010 will become 110011". "100111 will become "101000" 101101" will become "101110 Two's complement values are alao fixed in length at the time that they are created; the aame negative number may have multiple (slightly different) two's complement representationa basad on its final length in bits. For example, the 4-bit-long two's complement representation of -5 is 1011, while the 8-bit-long two's complement representation of -5 is 11111011. If you look closely, there'a a trick to this: in order to get the longer (more bits) version of the number, we simply extended (duplicated) the first (sign) bit to reach the deaired length. Perform this operation as follows: 1. Use Python's list) function to change your binary string into a list of one- character atrings. For example, list ( . 1 101 . } returns [ . 1,' 1''O'1' ]. 2. Follow the steps below to complete the tvosComplementfunction from the starter code. This function takes two arguments: a base 10 number to translate, and an integer length for the result (assume that this length will alwaya be big enough to accommodate the original value). The function returns the two's complement version of the original number as a string Use a while loop to examine the resulting list from the laat element (index length-1) to the first element. While the index ia greater than or equal to 0 and the element at the current index is '1', change that element to 0'and aubtract 1 from the index to keep moving toward the front of the list. 3. When the loop ends, if the index is greater than -1, change that element to 1 a If the original value is greater than or equal to 0, then the two's complement conversion 4. Use join) and the empty string to restore your list to a aingla string process is extremely simple: translate the original value into binary (base 2) normally and then "pad or extend the answer with leading 0s until it meets the desired length. Use changeBase) (from Part 1) to perform the initial translation, and then use a while loop to keep adding O' characters, one at a time, to the beginning of the result until it reaches the length apecified by the user (as the second parameter) answer.join(myliat) 3. After completely finishing Step 2(a) or Step 2b), return your final result (a atring of 03 and 13 whose length matches the second function parameter) For example, twoscomplement (25. 8) first converta 25 into base 2, geting the atring 11001". This string is only 5 characters long, not 8, ao we add three O' characters to the beginning to get "00011001" as our final answer. When you are finished, please submit your -py filea) through the "Lab 10" link on Blackboard. b. If the original value is negative, then we need to add a few more steps in order to produce the two's complement repreaentation: i Convert the absolute value of the original number into baae 2 and pad it, like you would have done in Step 2 a. For example, if the original value was -38, convert (positive) 38 to base 2 and pad the reault to the desired length. Use Python's built-in abs(function, or multiply the value by -1, before calling changeBase(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