几天前,我的老师告诉我可以使用位运算符检查给定点是否在给定矩形内。这是真的吗?如果是,该怎么做呢?
几天前,我的老师告诉我可以使用位运算符检查给定点是否在给定矩形内。这是真的吗?如果是,该怎么做呢?
Determine if a word has a byte between m and n
When m < n, this technique tests if a word x contains an unsigned byte value, such that m < value < n. It uses 7 arithmetic/logical operations when n and m are constant. Note: Bytes that equal n can be reported by likelyhasbetween as false positives, so this should be checked by character if a certain result is needed.
Requirements: x>=0; 0<=m<=127; 0<=n<=128
#define likelyhasbetween(x,m,n) \ ((((x)-~0UL/255*(n))&~(x)&((x)&~0UL/255*127)+~0UL/255*(127-(m)))&~0UL/255*128)
This technique would be suitable for a fast pretest. A variation that takes one more operation (8 total for constant m and n) but provides the exact answer is:
#define hasbetween(x,m,n) \ ((~0UL/255*(127+(n))-((x)&~0UL/255*127)&~(x)&((x)&~0UL/255*127)+~0UL/255*(127-(m)))&~0UL/255*128)
如果x0<x且x<x1且y0<y且y<y1
,则表示x,y
在矩形{x0<x<x1和y0<y<y1}
内。
如果我们可以使用位运算符模拟<
,那么就可以继续进行。
什么是二进制中的<
?考虑以下内容:
a: 0 0 0 0 1 1 0 1
b: 0 0 0 0 1 0 1 1
myBit!=otherBit
。(==或equiv
是按位运算符,可以用and/or/not表示)if(q,k,a,b) = if q[k] then a else b
等效的函数。答案是可以的:and
我们将其向右移动1位,并将其与未右移的版本进行or
。这将位于第一位置的位复制到第二位置。我们重复此过程,直到整个字与第一位相同(例如64次)mask
,我们的函数为(mask and a) or (!mask and b)
:如果q
的第k位为true,则结果将为a
,否则结果将为b
取位向量c=a!=b and a==1111..1 and b==0000..0
,我们使用我们的if
函数依次测试第一位是否为1,然后测试第二位是否为1,依此类推:a<b :=
if(c,0,
if(a,0, B_LESSTHAN_A, A_LESSTHAN_B),
if(c,1,
if(a,1, B_LESSTHAN_A, A_LESSTHAN_B),
if(c,2,
if(a,2, B_LESSTHAN_A, A_LESSTHAN_B),
if(c,3,
if(a,3, B_LESSTHAN_A, A_LESSTHAN_B),
if(...
if(c,64,
if(a,64, B_LESSTHAN_A, A_LESSTHAN_B),
A_EQUAL_B)
)
...)
)
)
)
)
wordsize
步骤。但是,如果使用递归定义的函数或固定点组合器(如果不允许使用递归),则可以将其写成3行。xMin<x and x<xMax and yMin<y and y<yMax
。Suppose we have a rectangle represented by the (a1,b1)
and (a2,b2)
. Given a point (x,y)
, we only need to evaluate the expression (a1<x) & (x<a2) & (b1<y) & (y<b2)
. So the problems now is to find the corresponding bit operation for the expression c
Let ci
be the i-th bit of the number c
(which can be obtained by masking ci
and bit shift). We prove that for numbers with at most n
bit, c<d
is equivalent to r_(n-1)
, where
r_i = ((ci^di) & ((!ci)&di)) | (!(ci^di) & r_(i-1))
Prove: When the ci
and di
are different, the left expression might be true (depends on ((!ci)&di)
), otherwise the right expression might be true (depends on r_(i-1)
which is the comparison of next bit).
The expression ((!ci)&di)
is actually equivalent to the bit comparison ci < di
. Hence, this recursive relation return true that it compares the bit by bit from left to right until we can decide c
is smaller than d
.
Hence there is an purely bit operation expression corresponding to the comparison operator, and so it is possible to find a point inside a rectangle with pure bitwise operation.
Edit: There is actually no need for condition statement, just expands the r_(n+1)
, then done.
reduce
或fold-right
函数,那完全是合法的,尽管有些晦涩难懂。至少,我可以定义一个递归函数使一切变得简洁。感谢您指出这一点。 - ninjagecko