.NET中是否有通用的BitArray?我只找到了非通用的.
可以有一个通用的BitArray吗?(即它是否合理?)
也许我应该说类型安全不通用.
基本上当你枚举类型时object,它应该不是int或bool?或者其中一个在另一个成员调查员中提供?
foreach (bool bit in myBitArray)
{
}
Run Code Online (Sandbox Code Playgroud)
我刚刚检查了BitArray类的枚举器,但是所有东西都返回了一个objectexcept .Current属性:
public virtual object Current
Run Code Online (Sandbox Code Playgroud) 我正在使用此代码向文件写一个大的bitarray:
import bitarray
bits = bitarray.bitarray(bin='0000011111') #just an example
with open('somefile.bin', 'wb') as fh:
bits.tofile(fh)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用以下方法读取此数据时:
import bitarray
a = bitarray.bitarray()
with open('somefile.bin', 'rb') as fh:
bits = a.fromfile(fh)
print bits
Run Code Online (Sandbox Code Playgroud)
它失败,'bits'是NoneType.我究竟做错了什么?
我正在尝试为学校项目实施erathostenes的筛选,我决定使用位数组来实现.在我搜索材料时,我遇到了这三个宏,它们完美无缺,但我无法真正阅读(理解)它们.
#define ISBITSET(x,i) ((x[i>>3] & (1<<(i&7)))!=0)
#define SETBIT(x,i) x[i>>3]|=(1<<(i&7));
#define CLEARBIT(x,i) x[i>>3]&=(1<<(i&7))^0xFF;
Run Code Online (Sandbox Code Playgroud)
能否请你详细解释其中至少一个,我对C中的按位操作有基本的了解(基本上我知道它们"存在").
这会在另一个使用不同字节序的架构上工作吗?提前致谢.
我有两个数字(从0到9),我想将它们组合成1个字节.数字1取0-3位,数字2取4-7位.
示例:我的编号为3和4.
3 = 0011,4是0100.
结果应为0011 0100.
如何使用这些二进制值创建一个字节?
这就是我目前拥有的:
public Byte CombinePinDigit(int DigitA, int DigitB)
{
BitArray Digit1 = new BitArray(Convert.ToByte(DigitA));
BitArray Digit2 = new BitArray(Convert.ToByte(DigitB));
BitArray Combined = new BitArray(8);
Combined[0] = Digit1[0];
Combined[1] = Digit1[1];
Combined[2] = Digit1[2];
Combined[3] = Digit1[3];
Combined[4] = Digit2[0];
Combined[5] = Digit2[1];
Combined[6] = Digit2[2];
Combined[7] = Digit2[3];
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,我有ArgumentOutOfBoundsExceptions
确定包含布尔值的向量(通常优化为位数组)是否仅包含真值的最快方法是什么?对于小向量,我认为将向量与另一个仅存储真实值的向量进行比较可能不是一个坏主意(假设我们知道两个向量的大小)。
我需要一个功能与vector<bool>C++ 相同的类.Rust文档说明了BitVec,但在编译期间use std::collections::BitVec导致未解决的导入错误.根据拉动请求,BitVec已被删除.有没有足够的替代品?
当我调用这段代码时:
BitArray bits = new BitArray(3);
bits[0] = true;
bits[1] = true;
bits[2] = true;
BitArray moreBits = new BitArray(3);
bits[0] = true;
bits[1] = true;
bits[2] = true;
BitArray xorBits = bits.Xor(moreBits);
foreach (bool bit in xorBits)
{
Console.WriteLine(bit);
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
真实的真实
当我在两个布尔值上执行xor时,通过说出true ^ true,我得到假.
代码有问题吗?我对XOR真值表的记忆是True XOR True是假的.
System.BitArray仅实现非通用IEnumerable,它返回IEnumerator.Current属性的Object.是否通过BitArray运行foreach - 例如
foreach (bool b in bitArray)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
框和unbox每个位值?
看看反射器中的bitarray枚举器,看起来它在每次调用MoveNext()时都会有一个新的位掩码而不是更聪明的东西.是否有更有效的方法来枚举BitArray,或者替换具有相同存储特性的BitArray?(List <bool> etc每个bool使用一个字节,而不是一个比特,因此使用8x的空间)
案例:
再次尝试通过我的NIC捕获数据包,
我开发了2个Extensions用于捕获可变数量的位
public static string ReadBits ( this BinaryReader Key , int Value )
{
BitArray _BitArray = new BitArray ( Value );
for ( int Loop = 0 ; Loop > Value ; Loop++ )
{
/* Problem HERE ---> */ _BitArray [ Loop ] = Key . ReadBoolean ( );
}
return BitConverter . ToString ( _BitArray . ToByteArray ( ) );
}
public static byte [ ] ToByteArray ( this BitArray Key )
{
byte [ …Run Code Online (Sandbox Code Playgroud) 我正在使用的BigInteger类型System.Numerics.
例如:
Dim Mult17 As BigInteger = BigInteger.Parse("1453453452342347")
Run Code Online (Sandbox Code Playgroud)
问题:
给定a BigInteger,如何将其转换为BitArray?(显然我希望BitArray保存二进制10101 ...表示存储在BigInteger变量中的数字,我希望可以选择以一种方式或相反的方式存储它.)
我还需要从a BitArray回到a BigInteger.
请注意,没有迹象表明:我只处理正整数.VB.NET或C#示例都没问题.谢谢.
bitarray ×10
c# ×5
.net ×2
biginteger ×1
binaryreader ×1
binarystream ×1
bit ×1
boolean ×1
byte ×1
c ×1
c++ ×1
fromfile ×1
ienumerable ×1
macros ×1
performance ×1
python ×1
rust ×1
stl ×1
vb.net ×1
vector ×1
xor ×1