GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
bitmap

Fix missing last bit read in bitmap_last_set_bit function

The `bitmap_last_set_bit` function now correctly identifies the last set bit in a bitmap, preventing it from missing the last bit in the first element.

The bitmap_last_set_bit function in bitmap.cc had a flaw where it would not correctly read the last bit if it happened to be in the first element of the bitmap. This could lead to incorrect results when searching for the last set bit. The fix ensures that the function now correctly handles this case, using a structure similar to bitmap_first_set_bit_worker to iterate through the bitmap.

In Details

The bitmap subsystem provides generic bitset data structures and operations. bitmap_last_set_bit is a function to find the index of the last set bit. The bug was in bitmap.cc where the function would fail to read the last bit in elt->bits[0], which is relevant for sparse bitmaps (sbitmap). This fix aligns the structure with bitmap_first_set_bit_worker for consistency.

For Context

Bitmaps are used to efficiently store and manipulate sets of bits. The bitmap_last_set_bit function finds the index of the last bit that is set to 1 in a bitmap. This commit fixes a bug in bitmap_last_set_bit where it could miss the last set bit if it was located in a specific part of the bitmap's underlying data structure. The fix ensures the function correctly identifies the last set bit in all cases.

Filed Under: gccbitmapbugfix