Python Forum
bitwise rotation/classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bitwise rotation/classes
#1
The assignment is to: take data from input file, input file has 3 sets of columns, decimal number, L or R for left/right direction, and a rather large number for # of places to shift.

What I have done so far is to create a class to get the decimals to binary, and one to reduce the large number of rotations.

This is the hang up, anytime I write a class which needs multiple arguments, for example this one will need my binary numbers, direction and # of shifts I can never get it to accept all arguments, please help me fix this.

Sample input for program:
13327 L 1063569662
29402 R 3981203893
16801 L 3151110067
56371 R 678511388
47472 R 1431378102
60535 R 3331940147
55948 L 3564833208
36426 L 841578072
28633 L 1488402849
30434 R 528505772

class dec2bin():
    
    def __init__(self, dec):
        self.dec = dec
        
    def __bin__(self, dec):
        return bin(dec, '016b')
        
    def __str__(self):
        return format(self.dec, '016b')

class cycles():
    
    def __init__(self, cycle):
        self.cycle = cycle

    def __mod__(self, cycle):
        cycle = self.cycle // 16
        return cycle

    def __str__(self):
        return str(self.cycle % 16)

class bit_shift():
    def __init__(self, direction, rotations):
        
        self.direction = direction
        self.rotations = rotations
        
    
    
def __bit_shift__(self):    
    for char in direction:
        if 'L' in char:
            rotated = bins << rotations
        else: 
            rotated = bins >> rotations    
    return rotated        
            
InFile = open('input.txt')

for lines in InFile:
    lines = lines.split()
    dec = dec2bin(int(lines[0]))
    direction = lines[1]
    cycle = cycles(int(lines[2]))
My code is probably not making sense but the first two classes do execute properly, instructor just introduced us to classes and then this assignment, i'm a bit overwhelmed by it, i understand the process of how it should work but, there's clearly a lot i'm not understanding about classes
Reply
#2
Hi, should it be assumed that the numbers to shift (e.g. 13327, 29402) are 16 bits long?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  exclusive bitwise OR bluefrog 1 2,173 Jul-10-2018, 12:32 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020