Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part II: Shifting Bits (4 points) ****IN PYTHON PLEASE******* A common operation

ID: 3756467 • Letter: P

Question

Part II: Shifting Bits (4 points) ****IN PYTHON PLEASE*******
A common operation within computer hardware involves shifting the bits in a memory location to the right or left by a certain
amount. When we perform one specific type of shift (an arithmetic shift) to the right, we remove some number of bits (n)
from the right end of the bit sequence, and replace them by inserting n copies of the first (leftmost) bit on the left. The end
result is a new sequence of bits with the same length as the original.
In pseudocode, we might describe this process as follows:
Use pop() in a loop, or slicing, to remove the last n bits from the end
Use insert() in a loop to insert n copies of the first bit at position 0
For example, suppose that we have the bit sequence 010011 and we with to shift this sequence 2 bits to the right. We
remove the two rightmost bits (11) and insert two copies of the first (leftmost) bit 0 at the beginning of the sequence (thus
preserving the original number of bits). This gives us the new bit sequence 000100.
Complete the rightShift() function, which takes two arguments: a list of 0s and 1s, and a positive integer representing
the number of positions to shift the contents of the list. If the shift amount is greater than or equal to the length of the list of
bits, the function returns without doing anything. Otherwise, the function should use the process above to directly modify
the list parameter; as with the previous problem, the function does not return any value on its own.
Examples:
Function Call    Final Contents of List
rightShift([0, 1, 1, 1, 0, 0, 0, 1], 3)    [0, 0, 0, 0, 1, 1, 1, 0]
rightShift([1, 1, 0, 1, 1], 1) [1, 1, 1, 0, 1]
rightShift([0, 0, 1, 0, 1, 1], 10)    [0, 0, 1, 0, 1, 1] (no change)

Explanation / Answer

Answer :

input_string = input('Enter the list of 1s and 0s separated by space: ')
input_list = input_string.split()
num = int(input("Enter the number of positions to shift the contents of the list: "))
assert (num > 0)
def rightShift(lst,n):
  
""" (list,int) -> None
modifies the list by performing right shift by n bits
>>>rightShift([1,0,1,0],2)
1110
>>>>rightShift([0,1,1,0],2)
0001
"""
  
if n >= len(lst):
return
else:
temp = lst[0]
for i in range(n):
lst.pop()
for i in range(n):
lst.insert(i,temp)
for i in range(len(lst)):
print (lst[i],end = " ")
  
rightShift(input_list,num)