Hum... Almost, but if the dim changes, you still need to mask the old pad bits located in the last old element. If think something more like this?
```D
            if (newlen > len)
            {   // Mask old MSBs
                auto mask = ~(~cast(size_t)0 << (len % bitsPerSizeT));
                if (newdim == olddim)
                {
                    auto mnewlen = newlen % bitsPerSizeT;
                    // Mask new pad bits
                    if (mnewlen)
                        mask |= ~cast(size_t)0 << mnewlen;
                }
                ptr[olddim - 1] &= mask;
            }
```
or
```D
            if (newlen > len)
            {   // Mask old MSBs
                auto mask = ~(~cast(size_t)0 << (len % bitsPerSizeT));
                auto mnewlen = newlen % bitsPerSizeT;
                if (newdim == olddim && mnewlen)
                    // Mask new pad bits
                    mask |= ~cast(size_t)0 << mnewlen;
                ptr[olddim - 1] &= mask;
            }
```