it is mentioned in the docs that this function Searches the Whole Bitmap for a run of required size but it starts Searching from the GivenHintIndex
quoted from doc
For a successful call, the returned bit position is not necessarily equivalent to the given HintIndex. If necessary, RtlFindClearBits searches the whole bitmap to locate a clear bit range of the requested size. However, it starts searching for the requested range from HintIndex, so callers can find such a range more quickly when they can supply appropriate hints about where to start looking.
you have probably Initilaized 2048 in RtlBitMapInitialize so it wraps around and returns 1
if you initialize say 2060 bits then you would get 2048 as result for all except the last one which will wrap around
changed code
RtlInitializeBitMap(&mybmap,bmapbuff,2060);
RtlClearAllBits(&mybmap);
RtlSetBits(&mybmap, 2047, 1);
RtlSetBits(&mybmap, 0, 1);
fcbret= RtlFindClearBits(&mybmap,1,2046); //2047
printf("fcbret = %u\n" , fcbret);
fcbret = RtlFindClearBits(&mybmap,3,2047); // 0 only 1 clear bit
printf("fcbret = %u\n" , fcbret);
fcbret = RtlFindClearBits(&mybmap,5,2047); // 0 only one clear bit
printf("fcbret = %u\n" , fcbret);
RtlSetBits(&mybmap,2040,3);
fcbret= RtlFindClearBits(&mybmap,1,2047); // 2047
printf("fcbret = %u\n" , fcbret);
fcbret = RtlFindClearBits(&mybmap,3,2040); // 2043 ,44, 45 are clear
printf("fcbret = %u\n" , fcbret);
fcbret = RtlFindClearBits(&mybmap,6,2040); // 0
printf("fcbret = %u\n" , fcbret);
fcbret = RtlFindClearBits(&mybmap,1741,300); // 0 2041 is set
printf("fcbret = %u\n" , fcbret);
results
:\>rtlbitmap.exe
ntdll loaded at 77370000
RtlInitBMap is at 773D5871
RtlFindClearBits is at 773D5DFF
RtlSetBits is at 773D5D0C
fcbret = 2046
fcbret = 2048
fcbret = 2048
fcbret = 2048
fcbret = 2043
fcbret = 2048
fcbret = 1
the 1 is because you have set 1 bit at index 0
if you change the second set to say
RtlSetBits(&mybmap, 6, 3);
the last findclear will wrap around and return you the index 9
: >rtlbitmap.exe
ntdll loaded at 77370000
RtlInitBMap is at 773D5871
RtlFindClearBits is at 773D5DFF
RtlSetBits is at 773D5D0C
fcbret = 2046
fcbret = 2048
fcbret = 2048
fcbret = 2048
fcbret = 2043
fcbret = 2048
fcbret = 9