I have the following code in Python:
for i in range(4):
if self.start == self.corners[i]:
self.visitedCorners += (1 << i)
I’m working with co-ordinates. self.start and self.corners are co-ordinates.
So with the code on the top I want to check whether the start is a corner.
If the start is the same of a corner, I do that shift. But, how does that shift work?
I don’t want any other code; I just want to understand this code.
All that
1 << idoes is produce the number with thei-th least significant bit set to1and all other bits set to0:In the code,
self.visitedCornersis a bit mask, where the four least significant bits correspond to the four corners. Each iteration of thefor iloop sets the corresponding bit inself.visitedCornersto1(provided theifcondition holds).