I’m trying to work out a logic for a game (in Flash). In one part, given the following row:
_ _ * _ _ __ *
I need to find if all the spaces between the *‘s are empty (Non-empty values other than * can occur in the row). This operation is done quite frequently.
I was wondering if I could use bit representations of rows to achieve this, instead of looping through and checking the intermediate positions.
For a row _ _ * _ _ _ * represented as xx1xxx1 (x = 1 or 0), I could AND it with 0001110 so that if the answer is 0000000, the intermediate positions are empty.
The question here is, of course, how to find this second map (0001110 above) using bit operations (in Flash AS2)? (Map (1,4) -> 0110, (1,3) -> 0100 etc)
Or is looping through the intermediate positions just the better choice?
You can do this by shifting some bits.
I’m not sure how you count to the positions, in the example it seems like you did it from the left, starting with a one – I’m going to count from the right, starting with zero, which is more common I guess. So the value of
startis the position of the first1(from the right, counting up from zero) andendthe position of the leftmost1.An example of how it works in detail:
Another way of describing this:
bits-(end+1)is the amount of zeroes on the left andstartis the amount of zeroes on the right of the sequence of ones.