0% found this document useful (0 votes)
35 views3 pages

Assignment 1 Arc

The document outlines a Python program that simulates register actions such as loading a binary string, shifting left or right, performing a circulate shift, and resetting the register. It defines a 'Register' class with methods for each action and a main function that provides a user interface for selecting actions. The program runs in a loop until the user chooses to exit.

Uploaded by

kwtalx1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

Assignment 1 Arc

The document outlines a Python program that simulates register actions such as loading a binary string, shifting left or right, performing a circulate shift, and resetting the register. It defines a 'Register' class with methods for each action and a main function that provides a user interface for selecting actions. The program runs in a loop until the user chooses to exit.

Uploaded by

kwtalx1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

: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

You might also like