我需要检查给定字节或字节序列中的特定位序列,如下所示:
- 可以以零个或多个
0开头。 - 可以从零个或多个
1开始。 - 末尾必须至少包含一个
0。
换句话说,如果字节的值不是0,那么我们只对包含连续的1后跟至少一个0的值感兴趣 最后。
我写了下面的代码来做到这一点,但想确保它高度优化。我觉得可以优化 if 分支中的多项检查,但不确定如何优化。请指教。
// The parameter [number] will NEVER be negative.
public static bool ConformsToPattern (System.Numerics.BigInteger number)
{
byte [] bytes = null;
bool moreOnesPossible = true;
if (number == 0) // 00000000
{
return (true); // All bits are zero.
}
else
{
bytes = number.ToByteArray();
if ((bytes [bytes.Length - 1] & 1) == 1)
{
return (false);
}
else
{
for (byte b=0; b < bytes.Length; b++)
{
if (moreOnesPossible)
{
if
(
(bytes [b] == 1) // 00000001
|| (bytes [b] == 3) // 00000011
|| (bytes [b] == 7) // 00000111
|| (bytes [b] == 15) // 00001111
|| (bytes [b] == 31) // 00011111
|| (bytes [b] == 63) // 00111111
|| (bytes [b] == 127) // 01111111
|| (bytes [b] == 255) // 11111111
)
{
// So far so good. Continue to the next byte with
// a possibility of more consecutive 1s.
}
else if
(
(bytes [b] == 128) // 10000000
|| (bytes [b] == 192) // 11000000
|| (bytes [b] == 224) // 11100000
|| (bytes [b] == 240) // 11110000
|| (bytes [b] == 248) // 11111000
|| (bytes [b] == 252) // 11111100
|| (bytes [b] == 254) // 11111110
)
{
moreOnesPossible = false;
}
else
{
return (false);
}
}
else
{
if (bytes [b] > 0)
{
return (false);
}
}
}
}
}
return (true);
}
重要:发送到函数的参数 [number] 将永远为负,因此无需检查符号位。
请您参考如下方法:
我要说的是,这些答案都没有说明
00000010
00000110
00001110
00011110
00111110
01111110
00000100
00001100
00011100
00111100
01111100
等等等等
这是我的字节数组方法:
public static bool ConformsToPattern(System.Numerics.BigInteger number)
{
bool foundStart = false, foundEnd = false;
int startPosition, stopPosition, increment;
if (number.IsZero || number.IsPowerOfTwo)
return true;
if (!number.IsEven)
return false;
byte[] bytes = number.ToByteArray();
if(BitConverter.IsLittleEndian)
{
startPosition = 0;
stopPosition = bytes.Length;
increment = 1;
}
else
{
startPosition = bytes.Length - 1;
stopPosition = -1;
increment = -1;
}
for(int i = startPosition; i != stopPosition; i += increment)
{
byte n = bytes[i];
for(int shiftCount = 0; shiftCount < 8; shiftCount++)
{
if (!foundEnd)
{
if ((n & 1) == 1)
foundEnd = true;
n = (byte)(n >> 1);
continue;
}
if (!foundStart)
{
if ((n & 1) == 0)
foundStart = true;
n = (byte)(n >> 1);
continue;
}
if (n == 0)
continue;
return false;
}
}
if (foundEnd)
return true;
return false;
}
这是我的 BigInteger 方法:
public static bool ConformsToPattern(System.Numerics.BigInteger number)
{
bool foundStart = false;
bool foundEnd = false;
if (number.IsZero || number.IsPowerOfTwo)
return true;
if (!number.IsEven)
return false;
while (!number.IsZero)
{
if (!foundEnd)
{
if (!number.IsEven)
foundEnd = true;
number = number >> 1;
continue;
}
if (!foundStart)
{
if (number.IsEven)
foundStart = true;
number = number >> 1;
continue;
}
return false;
}
if (foundEnd)
return true;
return false;
}
选择最适合您的那个。到目前为止,字节数组更快。 BigIntegers 代码是 100% 准确的引用。
如果您不担心 native 字节顺序,请删除该部分代码,但将其保留在那里将确保可移植到 x86 系统以外的其他系统。 BigIntegers 已经给出了 IsZero、IsEven 和 IsPowerOfTwo,所以这不是额外的计算。我不确定这是否是向右移位的最快方法,因为有一个 byte 到 int 的转换,但现在,我找不到另一种方法。至于使用 byte vs short vs int vs long 循环操作,如果你觉得它会更好用。我不确定您将发送哪种 BigIntegers,所以我认为 int 是安全的。您可以修改代码以删除 for 循环 并将代码复制粘贴 8 次,这样可能会更快。或者您可以将其放入静态方法中。
