Question: 8:44 Inc Labo3 - Read-only Read Only - You can't save changes to t... v CIS 101, Lab Exercise 3 8:44 CE S8 D Read

 8:44 Inc Labo3 - Read-only Read Only - You can't save

changes to t... v CIS 101, Lab Exercise 3 8:44 CE S8

D Read Only - You can't save changes to t... v The
doc t or s 247 . which we 13 y D i

8:44 Inc Labo3 - Read-only Read Only - You can't save changes to t... v CIS 101, Lab Exercise 3 8:44 CE S8 D Read Only - You can't save changes to t... v The doc t or s 247 . which we 13 y D i gure challenging + ME 67% 11:35 PM Thu Feb 6 X Labo3.pdf CIS 101, Lab Exercise 3 Partner You will be assigned a partner in the lab to work with on this exercise. Two people should work at onc computer. Occasionally switch the person who is typing. Talk to each other about what you are doing and why. The Problem We are going to do some conversions, from integer to binary and then from binary back to integer. It will give us a chance to play with if-elif-else and while statements, as well as a little string slicing Your Task You prompt for an integer, convert the integer to a binary number string (there is no type for actual binary numbers so we just represent it as a string). We then take the string and turn it back into a regular integer. Things to remember 1. If the integer is 0, then we are done since conversion back and forth of O is still o. The program simply prints a note saying it is 0 and quits. 2. If the integer is negative, then we probably don't know how to do it, so the program prints a message saying it is negative and quits. 3. Otherwise, we do the conversion of the integer to a binary string (a string of I's and O's) and then convert that same string back to an integer to make sure we did it right How do we get a binary string from an integer? The easiest method uses integer division by 2 on successive quotients and then collects the remainders. It is best illustrated by an example. Consider the decimal number 156. 156 divided by 2 gives the quotient 78 and remainder 0. 78 divided by 2 gives the quotient 39 and remainder 0. 39 divided by 2 gives the quotient 19 and remainder 1. 19 divided by 2 gives the quotient 9 and remainder 1. 9 divided by 2 gives the quotient 4 and remainder 1. 4 divided by 2 gives the quotient 2 and remainder 0. 2 divided by 2 gives the quotient 1 and remainder 0. 1 divided by 2 gives the quotient and remainder 1. Stop at reaching a quotient of. The binary equivalent is given by concatenating the remainders, in reverse (so the last remainder is the most significant bit and the first is the least). In this example: "10011100 How do we get an integer from a binary string? First, we know it is a string, so the elements are 'I' and 'o'. For every l in this string, we add a power of two to the overall decimal value. The power of 2 that we add depends on the position of the 1 in the binary string. Al in the far right (last) position of the string adds 2**0, in the next to last position adds 2** 1. in the next to the next to the last position adds 2** 2. and so on. If the bit is a '1', then we add that power of 2 to the overall sum; if it is o we do nothing

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!