Aug-25-2020, 12:58 AM
Hi - New to Python so please forgive any mistakes
I want to know if there is a more elegant way of slicing a byte?
I have an 8 bit result from reading a Port Expander (4 MSB's are where my inputs are connected)
In_1, In_2, In_3, In_4 will be used in the main program
I was looking for some sort of alias command where I could specify that In_1 = result.4 or something similar but I've not been able to find one.
Thanks for the help - still at the bottom of the learning curve
I want to know if there is a more elegant way of slicing a byte?
I have an 8 bit result from reading a Port Expander (4 MSB's are where my inputs are connected)
In_1, In_2, In_3, In_4 will be used in the main program
def input_status(result): #gets a byte value and returns input status result = result | 15 # mask out the bottom 4 bits (not really needed) result = result >>4 #only interested in the 4 MSBs In_1 = result & 1 #check bit 0 result = result >>1 #shift 1 place right In_2 = result & 1 #check bit 1 result = result >>1 #shift 1 place right In_3 = result & 1 #check bit 2 I_result = result >>1 #shift 1 place right In_4 = I_result & 1 #check bit 3 return (In_1, In_2, In_3, In_4)This works fine, but seems like a very cumbersome way of doing things?
I was looking for some sort of alias command where I could specify that In_1 = result.4 or something similar but I've not been able to find one.
Thanks for the help - still at the bottom of the learning curve
