public static void Main(string[]args)
{
int[] data = new int[10000000];
/*alias*/
Random r = new Random();
for (int i = 0; i < data.Length; i++)
{
data[i] = r.Next(1, 10000000);
}
Stopwatch stop = new Stopwatch();
stop.Start();
List<byte> lstbyte = new List<byte>();
foreach (var item in data)
{
int unit = item / 8;
int index = item % 8;
if (lstbyte.Count <= unit)
{
lstbyte.AddRange(new byte[unit - lstbyte.Count + 1]);
}
lstbyte[unit] = set_bit(lstbyte[unit], index + 1, true);
}
List<int> result = new List<int>();
for (int i = 0; i < lstbyte.Count; i++)
{
int currentIndex = i * 8;
List<int> lstint = new List<int>();
if (lstbyte[i] > 0)
{
int b = lstbyte[i] & 1;
if (b == 1)
{
lstint.Add(currentIndex + 0);
}
b = lstbyte[i] & 2;
if (b == 2)
{
lstint.Add(currentIndex + 1);
}
b = lstbyte[i] & 4;
if (b == 4)
{
lstint.Add(currentIndex + 2);
}
b = lstbyte[i] & 8;
if (b == 8)
{
lstint.Add(currentIndex + 3);
}
b = lstbyte[i] & 16;
if (b == 16)
{
lstint.Add(currentIndex + 4);
}
b = lstbyte[i] & 32;
if (b == 32)
{
lstint.Add(currentIndex + 5);
}
b = lstbyte[i] & 64;
if (b == 64)
{
lstint.Add(currentIndex + 6);
}
b = lstbyte[i] & 128;
if (b == 128)
{
lstint.Add(currentIndex + 7);
}
}
result.AddRange(lstint);
}
stop.Stop();
Console.WriteLine("结果数:" + result.Count);
//foreach (var item in result)
//{
// Console.WriteLine(item);
//}
Console.WriteLine(string.Concat("时间:", stop.ElapsedMilliseconds, "毫秒"));
Console.ReadKey();
}
/// <summary>
/// 设置某一位的值
/// </summary>
/// <param name="data"></param>
/// <param name="index">要设置的位, 值从低到高为 1-8</param>
/// <param name="flag">要设置的值 true / false</param>
/// <returns></returns>
static byte set_bit(byte data, int index, bool flag)
{
if (index > 8 || index < 1)
throw new ArgumentOutOfRangeException();
int v = index < 2 ? index : (2 << (index - 2));
return flag ? (byte)(data | v) : (byte)(data & ~v);
}