This should work, using a BitArray
to help you get the values, and this snippet from Jon Skeet to convert that to a byte[]
.
int[] ints = new[] { 1,0,0,0,0,0,1,1,1,1 };
// 1,0,0,... becomes true,false,false,... with this Select
BitArray bits = new BitArray(ints.Select(x => x > 0).ToArray());
byte[] bytes = new byte[(bits.Length + 7) / 8];
bits.CopyTo(bytes, 0);
BigInteger bigInt = new BigInteger(bytes); // 961
If performance is critical, you could probably improve it by building your byte[]
using bit shifting. But this is decently (IMO) concise, readable, and (I'd expect) fast code as-is.
558 (0,1,1,1,0,1,0,0,0,1
) works, too.