229k views
3 votes
A bit shift is a procedure whereby the bits in a bit string are moved to the left or to the right.

For example, we can shift the bits in the string 1011 two places to the left to produce the string 1110. Note that the leftmost two bits are wrapped around to the right side of the string in this operation.
Define two scripts, shiftLeft.py and shiftRight.py, that expect a bit string as an input.
The script shiftLeft shifts the bits in its input one place to the left, wrapping the leftmost bit to the rightmost position.
The script shiftRight performs the inverse operation.
Each script prints the resulting string.
An example of shiftLeft.py input and output is shown below:
Enter a string of bits: Hello world!
ello world!H
An example of shiftRight.py input and output is shown below:
Enter a string of bits: Hello world!
!Hello world

1 Answer

5 votes

Answer:

Following are the code to this question:

Defining method shiftLeft:

def shiftLeft(bit_string): #defining method shiftLeft, that accepts parameter bit_string

bit_string= bit_string[1:]+bit_string[0]#use bit_string to provide slicing

return bit_string#return bit_string value

bit_string =input("Enter value: ")#defining bit_string variable for user input

print (shiftLeft(bit_string))#use print method to call shiftLeft method

Defining method shiftRight:

def shiftRight(bit_string):#defining method shiftRight, which accepts bit_string variable

bit_string=bit_string[len(bit_string)-1]+bit_string[0:len(bit_string)-1]#using bit_string variable for slicing

return bit_string#return bit_string calculated value

bit_string= input("Enter value: ")#defining bit_string variable for user input

print(shiftRight(bit_string))#use print method to call shiftLeft method

Output:

Please find the attachment.

Step-by-step explanation:

method description:

  • In the above-given python code two methods "shiftLeft and shiftRight" are declared, in which both the method accepts a string variable "bit_string".
  • Inside methods, we use the slicing, in which it provides to use all the sequence values and calculated the value in this variable and return its value.
  • At the last step, the bit_string variable is used to input value from the user end and call the method to print its value.
A bit shift is a procedure whereby the bits in a bit string are moved to the left-example-1
User NSCry
by
5.9k points