Repository: dotnet/runtime
File: src/libraries/System.Private.CoreLib/src/System/Collections/BitArray.cs (C#)

Code before:
        public int TrailingZeroCount()
        {
            int count = 0;
            foreach (byte b in _array)
            {
                if (b != 0)
                {
                    return byte.TrailingZeroCount(b) + count;
                }

                count += BitsPerByte;
            }

            return _bitLength;
        }

        /// <summary>Computes the number of leading zero bits in the <see cref="BitArray"/>.</summary>
        /// <returns>The number of leading zero bits in the <see cref="BitArray"/>.</returns>
        public int LeadingZeroCount()
        {
            int count = _bitLength - _array.Length * BitsPerByte; // offset for extra bits beyond _bitLength
            for (int i = _array.Length - 1; i >= 0; i--)
            {
                if (_array[i] != 0)
                {
                    return byte.LeadingZeroCount(_array[i]) + count;
                }

                count += BitsPerByte;
            }

            return _bitLength;
        }

        /// <summary>Gets the number of elements contained in the <see cref="BitArray"/>.</summary>
        public int Count => _bitLength;

        /// <summary>Gets an object that can be used to synchronize access to the <see cref="BitArray"/>.</summary>
        public object SyncRoot => this;

        /// <summary>Gets a value indicating whether access to the <see cref="BitArray"/> is synchronized (thread safe).</summary>
        public bool IsSynchronized => false;

        /// <summary>Gets a value indicating whether the <see cref="BitArray"/> is read-only.</summary>
        public bool IsReadOnly => false;

        /// <summary>Creates a shallow copy of the <see cref="BitArray"/>.</summary>
        public object Clone() => new BitArray(this);

        /// <summary>Returns an enumerator that iterates through the <see cref="BitArray"/>.</summary>
        /// <returns>An IEnumerator for the entire <see cref="BitArray"/>.</returns>

Review comment (suggestion):
This is exposing 4-byte storage alignment that is an internal implementation detail. Are there any other APIs on BItArray leak this internal implementation detail?

I would expect the leading zero-bit count to be leading zero-bits count in the most significant byte, and ignore any internal storge alignment.

(The concept of leading zero-bit count is a bit question for a variable sized "number".)

Code after:
            private readonly int _version;
            private int _index;
            private object _currentElement = s_boxedFalse;

            internal BitArrayEnumeratorSimple(BitArray bitArray)
            {
                _bitArray = bitArray;
                _index = -1;
                _version = bitArray._version;
            }

            public object Clone() => MemberwiseClone();

            public bool MoveNext()
            {
                if (_version != _bitArray._version)
                {
                    throw new InvalidOperationException(SR.InvalidOperation_EnumFailedVersion);
                }

                if (_index < (_bitArray._bitLength - 1))
                {
                    _index++;
                    _currentElement = _bitArray.Get(_index) ? s_boxedTrue : s_boxedFalse;
                    return true;
                }

                _index = _bitArray._bitLength;
                return false;
            }

            public object Current
            {
                get
                {
                    if ((uint)_index >= (uint)_bitArray._bitLength)
                    {
                        throw GetInvalidOperationException(_index);
                    }

                    return _currentElement;
                }
            }

            public void Reset()
            {
                if (_version != _bitArray._version)
                {
                    throw new InvalidOperationException(SR.InvalidOperation_EnumFailedVersion);
                }