RtlClearBits - NtDoc

Native API online documentation, based on the System Informer (formerly Process Hacker) phnt headers
#ifndef _NTRTL_H
/**
 * The RtlClearBits routine sets all bits in the specified range of bits in the bitmap to zero.
 *
 * \param BitMapHeader A pointer to the RTL_BITMAP structure that describes the bitmap. This structure must have been initialized by the RtlInitializeBitMap routine.
 * \param StartingIndex The index of the first bit in the bit range that is to be cleared. If the bitmap contains N bits, the bits are numbered from 0 to N-1.
 * \param NumberToClear Specifies how many bits to clear. If the bitmap contains N bits, this parameter can be a value in the range 1 to (N - StartingIndex).
 * \remarks If the NumberToClear parameter is zero, RtlClearBits simply returns control without clearing any bits. The sum (StartingIndex + NumberToClear) must not exceed the SizeOfBitMap parameter value specified in the RtlInitializeBitMap call that initialized the bitmap.
 * \sa https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlclearbits
 */
#ifndef PHNT_NO_INLINE_RTL_BITMAP

FORCEINLINE
VOID
NTAPI_INLINE
RtlClearBits(
    _In_ PRTL_BITMAP BitMapHeader,
    _In_range_(0, BitMapHeader->SizeOfBitMap - NumberToClear) ULONG StartingIndex,
    _In_range_(0, BitMapHeader->SizeOfBitMap - StartingIndex) ULONG NumberToClear
    )
{
    PUCHAR buffer;
    ULONG offset;
    ULONG count;
    UCHAR mask;
    SIZE_T bytes;

    if (NumberToClear == 0)
        return;

    buffer = (PUCHAR)BitMapHeader->Buffer + (SIZE_T)(StartingIndex >> 3);
    offset = (ULONG)(StartingIndex & 7);

    if (offset != 0)
    {
        count = 8 - offset;
        if (NumberToClear < count)
            count = (ULONG)NumberToClear;
        mask = (UCHAR)(((1u << count) - 1) << offset);
        *buffer++ &= (UCHAR)~mask;
        NumberToClear -= count;
    }

    bytes = (SIZE_T)(NumberToClear >> 3);
    if (bytes != 0)
    {
        memset(buffer, 0, bytes);
        buffer += bytes;
    }

    count = (ULONG)(NumberToClear & 7);
    if (count != 0)
    {
        mask = (UCHAR)((1u << count) - 1);
        *buffer &= (UCHAR)~mask;
    }
}

#endif
#endif

View code on GitHub
#ifndef _NTRTL_H
/**
 * The RtlClearBits routine sets all bits in the specified range of bits in the bitmap to zero.
 *
 * \param BitMapHeader A pointer to the RTL_BITMAP structure that describes the bitmap. This structure must have been initialized by the RtlInitializeBitMap routine.
 * \param StartingIndex The index of the first bit in the bit range that is to be cleared. If the bitmap contains N bits, the bits are numbered from 0 to N-1.
 * \param NumberToClear Specifies how many bits to clear. If the bitmap contains N bits, this parameter can be a value in the range 1 to (N - StartingIndex).
 * \remarks If the NumberToClear parameter is zero, RtlClearBits simply returns control without clearing any bits. The sum (StartingIndex + NumberToClear) must not exceed the SizeOfBitMap parameter value specified in the RtlInitializeBitMap call that initialized the bitmap.
 * \sa https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlclearbits
 */
#ifndef PHNT_NO_INLINE_RTL_BITMAP
// ...
#else

NTSYSAPI
VOID
NTAPI
RtlClearBits(
    _In_ PRTL_BITMAP BitMapHeader,
    _In_range_(0, BitMapHeader->SizeOfBitMap - NumberToClear) ULONG StartingIndex,
    _In_range_(0, BitMapHeader->SizeOfBitMap - StartingIndex) ULONG NumberToClear
    );

#endif
#endif

View code on GitHub
// wdm.h

NTSYSAPI VOID RtlClearBits(
  [in] PRTL_BITMAP BitMapHeader,
  [in] ULONG       StartingIndex,
  [in] ULONG       NumberToClear
);
View the official Windows Driver Kit DDI reference

NtDoc

This function is documented in Windows Driver Kit.

Windows Driver Kit DDI reference (nf-wdm-rtlclearbits)

RtlClearBits function

Description

The RtlClearBits routine sets all bits in the specified range of bits in the bitmap to zero.

Parameters

BitMapHeader [in]

A pointer to the RTL_BITMAP structure that describes the bitmap. This structure must have been initialized by the RtlInitializeBitMap routine.

StartingIndex [in]

The index of the first bit in the bit range that is to be cleared. If the bitmap contains N bits, the bits are numbered from 0 to N-1.

NumberToClear [in]

Specifies how many bits to clear. If the bitmap contains N bits, this parameter can be a value in the range 1 to (N - StartingIndex).

Return value

This routine does not return a value.

Remarks

If the NumberToClear parameter is zero, RtlClearBits simply returns control without clearing any bits.

The sum (StartingIndex + NumberToClear) must not exceed the SizeOfBitMap parameter value specified in the RtlInitializeBitMap call that initialized the bitmap.

Callers of RtlClearBits must be running at IRQL <= APC_LEVEL if the memory that contains the bitmap is pageable or the memory at BitMapHeader is pageable. Otherwise, RtlClearBits can be called at any IRQL.

See also

RTL_BITMAP

RtlAreBitsSet

RtlClearAllBits

RtlFindSetBits

RtlFindSetBitsAndClear

RtlInitializeBitMap

RtlNumberOfSetBits