public static int getIntegerFromBitArray(BitArray bitArray)
{
var result = new int[1];
bitArray.CopyTo(result, 0);
return result[0];
}
// Input A) 01110
// Output A) 14
// Input B) 0011
// Output B) 12 <=== ????? WHY!!! :)谁能解释一下为什么我的第二个返回值是12而不是3?请..。谢谢。
发布于 2012-03-14 15:37:25
基本上,它考虑的位的顺序与您预期的相反-您还没有展示如何将输入的二进制映射到BitArray,但结果是将其视为1100而不是0011。
无可否认,文档并不清楚,但它确实以我期望的方式工作:bitArray[0]表示最低有效值,就像讨论二进制时通常的情况一样(因此位0是0/1,位1是0/2,位2是0/4,位3是0/8等等)。例如:
using System;
using System.Collections;
class Program
{
static void Main(string[] args)
{
BitArray bits = new BitArray(8);
bits[0] = false;
bits[1] = true;
int[] array = new int[1];
bits.CopyTo(array, 0);
Console.WriteLine(array[0]); // Prints 2
}
}发布于 2012-03-14 15:40:25
你需要将钻头旋转到正确的方向才能得到正确的结果。1100等于12
https://stackoverflow.com/questions/9697623
复制相似问题