/// <summary> /// Set bits to desired boolean value. /// </summary> /// <param name="pos">Position in bit array.</param> /// <param name="value">Value of bits to set.</param> /// <param name="numBits">Number of bits to set.</param> public void SetBits(int pos, bool value, int numBits) { CheckArgs(pos, numBits); var end = math.min(pos + numBits, Length); var idxB = pos >> 6; var shiftB = pos & 0x3f; var idxE = (end - 1) >> 6; var shiftE = end & 0x3f; var maskB = 0xfffffffffffffffful << shiftB; var maskE = 0xfffffffffffffffful >> (64 - shiftE); var orBits = (ulong)-Bitwise.FromBool(value); var orBitsB = maskB & orBits; var orBitsE = maskE & orBits; var cmaskB = ~maskB; var cmaskE = ~maskE; if (idxB == idxE) { var maskBE = maskB & maskE; var cmaskBE = ~maskBE; var orBitsBE = orBitsB & orBitsE; Ptr[idxB] = (Ptr[idxB] & cmaskBE) | orBitsBE; return; } Ptr[idxB] = (Ptr[idxB] & cmaskB) | orBitsB; for (var idx = idxB + 1; idx < idxE; ++idx) { Ptr[idx] = orBits; } Ptr[idxE] = (Ptr[idxE] & cmaskE) | orBitsE; }
/// <summary> /// Set single bit to desired boolean value. /// </summary> /// <param name="pos">Position in bit array.</param> /// <param name="value">Value of bits to set.</param> public void Set(int pos, bool value) { CheckArgs(pos, 1); var idx = pos >> 6; var shift = pos & 0x3f; var mask = 1ul << shift; var bits = (Ptr[idx] & ~mask) | ((ulong)-Bitwise.FromBool(value) & mask); Ptr[idx] = bits; }