digitalmars.D.learn - converting a number into bit array
Given this, ´´´ import std.bitmanip; import core.stdc.limits; void main() { BitArray ba; ba.length = size_t.sizeof * CHAR_BIT; // enough length, known at compile time size_t arbitrary; // = random size_t, not known at compile time // ba ???assign??? arbitrary; assert(cast(size_t[])ba == [arbitrary]); } ´´´ 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?
Mar 25 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)); } ´´´
Mar 25 2018