result.ptr[dim - 1] &= ~(~0 << (len & (bitsPerSizeT-1))); On 64 bit machine 0 - is 32 bit value, but bitsPerSizeT == 64. Need add cast 0 to size_t: result.ptr[dim - 1] &= ~(~cast(size_t)0 << (len & (bitsPerSizeT-1))); Test: foreach (len; 1 .. 256) { foreach (i; 0 .. len) { BitArray a1; a1.length = len; foreach (j; 0 .. len) { a1[j] = true; } a1[i] = false; BitArray a2; a2.length = len; a2[i] = true; BitArray a3 = ~a2; assert(a3 == a1); } } For reproduce you need fix opEquals (see bug #10948: http://d.puremagic.com/issues/show_bug.cgi?id=10948)
https://github.com/D-Programming-Language/phobos/pull/2249