Write a simple python program to do the following calculation. Ask the user for
ID: 3645709 • Letter: W
Question
Write a simple python program to do the following calculation. Ask the user for two input values. The first should be a positive integer value of any number of digits entered as a string. Print an error message if the input is not a positive integer. This value should be converted into its corresponding binary representation as a string. The second input value should be an integer indicating the number of bit positions to shift this value to either the right or left. A positive value means shift right, a negative value means shift left. Again, print an error message if an integer value is not input.As bits are shifted off the end of the binary value according to the shift amount, they should be rotated into the other end. For example, as the bits are shifted to the right, the rightmost one should move to the left end of the string. After you have shifted the bits the proper amount, you should convert the new value into its hexadecimal equivalent, as well as its decimal equivalent. You should print out the original integer value, its binary equivalent, the shifted binary value, its hexadecimal equivalent and its decimal equivalent.
Explanation / Answer
Answer: modulus operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%). The syntax is the same as for other operators: >>> quotient = 7 / 3 >>> print quotient 2 >>> remainder = 7 % 3 >>> print remainder 1 So 7 divided by 3 is 2 with 1 left over. The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another if x % y is zero, then x is divisible by y.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.