Question

A bit shift is a procedure whereby the bits in a bit string are moved to...

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 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Provided code is done as per your requirements.

  • "Screen shot program code" for better understanding.
  • "Code to copy" for run the program in your IDE.

shiftLeft.py

Code Image:

#Implementation of shiftLeft method #which shifts the bits in its input one #place to tge left def shiftLeft (bitstring): bitstringbitstring[1:1+bitstring[0] #return as bit string format return bitstring #Get the input from user bits = input (Enter a string f bits: ) #call the shiftLeft method which returns the value # that is stored in leftShift leftShíft = shiftLeft (bits) #Display the output print () print (leftShift) print ()

Sample Output:

Code to Copy:

#Implementation of shiftLeft method

#which shifts the bits in its input one

#place to tge left

def shiftLeft(bitstring):

    bitstring = bitstring[1:]+bitstring[0]

    #return as bit string format

    return bitstring

#Get the input from user

bits = input("Enter a string of bits: ")

#call the shiftLeft method which returns the value

# that is stored in leftShift

leftShift = shiftLeft(bits)

#Display the output

print()

print(leftShift)

print()

shiftRight.py

Code Image:

Sample Output:

Code to Copy:

#Implementation of shiftRight method

#which performs inverse operation

def shiftRight(bitstring):

    bitstring=bitstring[len(bitstring)-1] + bitstring[0:len(bitstring)-1]

    return bitstring

#Get the input from user

bits = input("Enter a string of bits: ")

#call the shiftRight method which performs

#inverse operation

rightShift = shiftRight(bits)

#Display the output

print()

print(rightShift)

print()

Add a comment
Answer #2

Simple Version, Shift Left:


s = input("Enter a string of bits: ")

s = s[1:]+s[0]

print(s)


Shift Right:


a = input("Enter a string of bits: ")

a=a[len(a)-1] + a[0:len(a)-1]

print(a)

source: Python
answered by: King
Add a comment
Know the answer?
Add Answer to:
A bit shift is a procedure whereby the bits in a bit string are moved to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Exercise 4.6 Use the strategy of the decimal to binary conversion and the bit shift...

    Programming Exercise 4.6 Use the strategy of the decimal to binary conversion and the bit shift left operation defined in Project 5 bits = input("Enter bit string: ") bits = bits[1:] + bits[0] print ("Result:", bits) to code a new encryption algorithm. The algorithm should Add 1 to each character’s numeric ASCII value. Convert it to a bit string. Shift the bits of this string one place to the left. A single-space character in the encrypted string separates the resulting...

  • 2. Serial shift registers Draw missing connections to implement various shift registers 1. Shift right: All...

    2. Serial shift registers Draw missing connections to implement various shift registers 1. Shift right: All bits of the register move right by one position, and a new bit value from a serial input is stored in the most significant bit (leftmost flip-flop below). Serial input -02 az 02 a Do ao Serial indino 2. Shift left: All bits of the register move left by one position, and a new bit value from a serial input is stored in the...

  • python Programming Exercise 4.7 Write a script that decrypts a message coded by the method used...

    python Programming Exercise 4.7 Write a script that decrypts a message coded by the method used in Project 6. Method used in project 6: Add 1 to each character’s numeric ASCII value. Convert it to a bit string. Shift the bits of this string one place to the left. A single-space character in the encrypted string separates the resulting bit strings. An example of the program input and output is shown below: Enter the coded text: 0010011 1001101 1011011 1011011...

  • Need to finish the des_round method JAVA expands 6-bit r-input to 8-bits.  The 8 bits should be:...

    Need to finish the des_round method JAVA expands 6-bit r-input to 8-bits.  The 8 bits should be: (1) (2) (4) (3) (4) (3) (5) (6) of original 6-bit input generates round key based on the master key and round number passes the expanded_r and round key to f(), which: 3a. XORs them together; 3b. Splits result into two 4-bit sequences 3c. Refer to the 2 S-Boxes (row = bit 1; column = bits 2-4) to get two 3-bit outputs 3d. Concatenates...

  • Create a combinatorial circuit that performs a 3 bit multiplication. The circuit will have two 3-bit...

    Create a combinatorial circuit that performs a 3 bit multiplication. The circuit will have two 3-bit inputs A2 A1 A0 and B2 B1 B0 and one 6 bit output. Create a combinatorial circuit that performs a 3-bit multiplication. The circuit will accept two 3-bit inputs A2 A1 A0 and B2 B1 B0 and generate one 6 bit output. First, create 3 Integrated circuits (iC). The first IC takes as input, B0, A2, A1, and A0 as input, and it generates...

  • PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut...

    PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut the The program will ask the user to enter an unsigned also how many bits for the shift operation. Display the entered operation in both decimal and binary formats, vise shirt operators to shift the bits to the right >> or the left << user to enter an unsigned integer number, choose right shift or left shift, and orauon. Display the entered number and...

  • and then print their average. Write a C program that accepts a string of text from...

    and then print their average. Write a C program that accepts a string of text from the user and prints back the string without any of the vowels. For example, if the user entered "Hello, world", the program should print "Hll, wrld" Write a C program that accepts an integer in decimal from the user and prints the number of 1' bits in the integers binary representation. For example, if the user entered 14, the program should print 3 because...

  • A shift operation is often used to implement either multiply-by-power-of-2 or divide-by power-of-...

    A shift operation is often used to implement either multiply-by-power-of-2 or divide-by power-of-2 operations. For example, 0010 x4 1000. This multiplication can be achieved by shifting 0010 by 2 bits to the left. Likewise, 10004 0010, which can be obtained by shifting 1000 to the right by 2 bits. As multipliers are more "expensive" in terms of area and power consumption, multiplication by shifting is preferred if applicable. Sometime, the multiplier does not have to be power-of-2. For example, 0010...

  • 1) We would like to design a bus system for 32 registers of 16 bits each....

    1) We would like to design a bus system for 32 registers of 16 bits each. How many multiplexers are needed for the design? Select one: 5 16 1 4 32 2) The basic computer can be interrupted while another interrupt is being serviced. Select one: True False 3) If the Opcode bits of an instruction is 111, then the basic computer instruction type is either memory-reference or input-output. Select one: True False 4) The content of AC in the...

  • MIPS

    1.  Write an input-output utility io_lib.asm that contains the following subprograms:     a.  input_sub:  get user input          - prompt the user to enter an integer          - print the entered input as hexadecimal in the format of:  0xnnnnnnnn      b.  output_sub:  print output          - print a text string provided by the caller          - print the value in hexadecimal format 2.  Write a logic operation utility logic_lib.asm that contains the following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT