查找数字位置的位掩码
我正在尝试制定游戏的逻辑(在 Flash 中)。在一个部分中,给定以下行:
_ _ * _ _ __ *
我需要查找 *
之间的所有空格是否为空(非空值该行中可以出现 * 以外的内容)。 此操作执行得相当频繁。
我想知道是否可以使用行的位表示来实现此目的,而不是循环并检查中间位置。
对于表示为 xx1xxx1
(x
= 1 或 0)的行 _ _ * _ _ _ *
,我可以将其与 进行 AND 运算0001110
因此,如果答案是 0000000
,则中间位置为空。
当然,这里的问题是如何使用位操作(在 Flash AS2 中)找到第二个映射(上面的0001110
)? (地图(1,4)-> 0110,(1,3)-> 0100等)
或者循环遍历中间位置只是更好的选择?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过移动一些位来做到这一点。
我不确定你是如何计算位置的,在这个例子中,你似乎从左边开始计数,从一开始 - 我将从右边开始计数,从零开始,我想这更常见。所以
start
的值是第一个1
的位置(从右边开始,从零开始计数),而end
是最左边的位置<代码>1。详细说明其工作原理的示例:
另一种描述方式:
bits-(end+1)
是左侧零的数量,start
是左侧零的数量0 位于 1 序列的右侧。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
start
is the position of the first1
(from the right, counting up from zero) andend
the 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 andstart
is the amount of zeroes on the right of the sequence of ones.