I am working through a tutorial trying to learn Python. I have come across a line of code I cannot decipher.
I have asked the developer of the code and their explanation ignored my question. My question seems too basic for them to address.
I have simplified the code to the following:
GPIO.output(22,((j == 1<<i) and GPIO.HIGH or GPIO.LOW))
The index i rotates through 0 to 3
The index j rotates through 0x01, 0x02, 0x03 and 0x04
I know the code sends a "High" or "Low" to GPIO pin 22.
I do not understand the code: (j == 1<<i)
it seems to be checking for equality between a hex number "j" and "1<<i"
Questions:
1. How does this work? In other words, what is it saying?
2. Once evaluated, how does it interact with the following "and" and "or" statement?
3.Is there a simpler way to write this line of code?
I have asked the developer of the code and their explanation ignored my question. My question seems too basic for them to address.
I have simplified the code to the following:
GPIO.output(22,((j == 1<<i) and GPIO.HIGH or GPIO.LOW))
The index i rotates through 0 to 3
The index j rotates through 0x01, 0x02, 0x03 and 0x04
I know the code sends a "High" or "Low" to GPIO pin 22.
I do not understand the code: (j == 1<<i)
it seems to be checking for equality between a hex number "j" and "1<<i"
Questions:
1. How does this work? In other words, what is it saying?
2. Once evaluated, how does it interact with the following "and" and "or" statement?
3.Is there a simpler way to write this line of code?