converting a number into bit array

Alex sascha.orlov at gmail.com
Sun Mar 25 13:12:10 UTC 2018


On Sunday, 25 March 2018 at 11:32:56 UTC, Alex wrote:
> how to convert a number to a BitArray as fast as possible, 
> given that the BitArray is already allocated to the needed 
> length?
> Is bit checking the way to go, or is there a way to cast one to 
> the other somehow?

Via bit checking I would end with this:

´´´
void assign(BitArray ba, size_t val) //@nogc
{
	import std.algorithm : each;

	assert(ba.length == size_t.sizeof * CHAR_BIT);

	val.bitsSet.each!(b => ba.flip(b));
}
´´´


More information about the Digitalmars-d-learn mailing list