:Write a program that simulates the register actions¢
.Load from input -1
.Shift left a number of times entered by the user (do not forget to ask for the serial input) -2
Shift right a number of times entered by the user -3
.Circulate Shift -4
Reset Register to all -5
Solution by Python
:class Register
:def __init__(self)
self.value = 0 # Initialize the register value
:def load(self, binary_string)
""".Load a binary string into the register"""
self.value = int(binary_string, 2)
print(f"Loaded value: {self.get_binary_string()}")
:def shift_left(self, times)
""".Shift the register value left by a specified number of times """
self.value <<= times
print(f"Shifted left {times} times: {self.get_binary_string()}")
:def shift_right(self, times)
""".Shift the register value right by a specified number of times """
self.value >>= times
print(f"Shifted right {times} times: {self.get_binary_string()}")
:def circulate_shift(self)
""".Perform a circulate shift (rotate) on the register value """
)(binary_string = self.get_binary_string
:if len(binary_string) > 0
Rotate left #
rotated_value = binary_string[1:] + binary_string[0]
self.value = int(rotated_value, 2)
print(f"Circular shift: {self.get_binary_string()}")
:def reset(self)
""".Reset the register to all zeros"""
self.value = 0
print("Register reset to all zeros.")
:def get_binary_string(self)
""".Get the binary string representation of the register value """
return format(self.value, 'b')
:)(def main
)(register = Register
:while True
print("\nRegister Actions:")
print("1. Load from input")
print("2. Shift left")
print("3. Shift right")
print("4. Circulate Shift")
print("5. Reset Register to all zeros")
print("6. Exit")
choice = input("Choose an action (1-6): ")
:'if choice == '1
binary_input = input("Enter a binary number (e.g., 1010): ")
register.load(binary_input)
:'elif choice == '2
times = int(input("Enter number of times to shift left: "))
register.shift_left(times)
:'elif choice == '3
times = int(input("Enter number of times to shift right: "))
register.shift_right(times)
:'elif choice == '4
)(register.circulate_shift
:'elif choice == '5
)(register.reset
:'elif choice == '6
print("Exiting the program.")
break
:else
print("Invalid choice. Please try again.")
:"__if __name__ == "__main
)(main