获取位数组中 1 的索引?
Get index of a 1 in a bitarray?
朋友们,谁知道如何在Bitarray 中获取1 的索引并将其推送到数组中。一些功能或其他东西
我有一个 Uint16 ,在这里我想从这个变量中读取位并获取 1 的索引并将其放入数组或列表
您查询位数组的每个位置并报告索引。您可以使用简单的 for
循环并在列表中累积您的真实索引 - 我选择 linq,看起来不错:
using System.Linq;
using System.Collections;
public static IEnumerable<int> GetTrueIndexes(BitArray arr)
{
if (arr != null)
return Enumerable.Range(0,arr.Count).Where( idx => arr.Get(idx));
return new int[0];
}
public static void Main()
{
BitArray b = new BitArray(
"100101010000101"
.Select(c => c == '0' ? false : true )
.ToArray());
var trueIndexes = GetTrueIndexes(b);
System.Console.WriteLine(string.Join(", ",trueIndexes));
}
输出:
0、3、5、7、12、14
第 1 步,准备您的 BitArray:
var bits = new BitArray (new[] { false, true, false, false, true, false, false });
第2步,改成你能理解的形式(List, 1=true, 0=false)
var list = bits.Cast<bool> ().Select (x => x ? 1 : 0).ToList ();
第 3 步,现在您可以使用您已经知道的 IndexOf
int index = list.IndexOf (1); // index=1, it looks from left ot right
如果您想从右到左搜索,请在列表中使用 Reverse()
方法。
这不是最优解,但我认为它对你来说是最容易理解的。
编辑:
var bits = new BitArray (new[] { false, true, false, false, true, false, false });
var bitsWithIndex = bits.Cast<bool> () // we need to use Cast because BitArray does not provide generic IEnumerable
.Select ((bit, index) => new { Bit = bit, Index = index}); // projection, we will save bit indices
// now we will get indices of all true(1) bits [from left to right]
var indices = bitsWithIndex.Where (x => x.Bit == true).Select (x => x.Index).ToArray ();
你有一个 UInt16,你需要读取位 1 的索引然后:
List<int> GetIndexes(int number)
{
var result = new List<int>();
var index = 0;
while (number > 0)
{
if (number & 1)
{
result.Add(index);
}
index ++;
number >= 1;
}
return result;
}
朋友们,谁知道如何在Bitarray 中获取1 的索引并将其推送到数组中。一些功能或其他东西
我有一个 Uint16 ,在这里我想从这个变量中读取位并获取 1 的索引并将其放入数组或列表
您查询位数组的每个位置并报告索引。您可以使用简单的 for
循环并在列表中累积您的真实索引 - 我选择 linq,看起来不错:
using System.Linq;
using System.Collections;
public static IEnumerable<int> GetTrueIndexes(BitArray arr)
{
if (arr != null)
return Enumerable.Range(0,arr.Count).Where( idx => arr.Get(idx));
return new int[0];
}
public static void Main()
{
BitArray b = new BitArray(
"100101010000101"
.Select(c => c == '0' ? false : true )
.ToArray());
var trueIndexes = GetTrueIndexes(b);
System.Console.WriteLine(string.Join(", ",trueIndexes));
}
输出:
0、3、5、7、12、14
第 1 步,准备您的 BitArray:
var bits = new BitArray (new[] { false, true, false, false, true, false, false });
第2步,改成你能理解的形式(List, 1=true, 0=false)
var list = bits.Cast<bool> ().Select (x => x ? 1 : 0).ToList ();
第 3 步,现在您可以使用您已经知道的 IndexOf
int index = list.IndexOf (1); // index=1, it looks from left ot right
如果您想从右到左搜索,请在列表中使用 Reverse()
方法。
这不是最优解,但我认为它对你来说是最容易理解的。
编辑:
var bits = new BitArray (new[] { false, true, false, false, true, false, false });
var bitsWithIndex = bits.Cast<bool> () // we need to use Cast because BitArray does not provide generic IEnumerable
.Select ((bit, index) => new { Bit = bit, Index = index}); // projection, we will save bit indices
// now we will get indices of all true(1) bits [from left to right]
var indices = bitsWithIndex.Where (x => x.Bit == true).Select (x => x.Index).ToArray ();
你有一个 UInt16,你需要读取位 1 的索引然后:
List<int> GetIndexes(int number)
{
var result = new List<int>();
var index = 0;
while (number > 0)
{
if (number & 1)
{
result.Add(index);
}
index ++;
number >= 1;
}
return result;
}