Nice bit twiddling hack! There are also the PDEP and PEXT instructions on x86 that could be used for this purpose, but it's possibly not worth it here.
Am I missing something or does that instruction only clear bits in the field and not shift the adjacent bits to contract the field to be empty?
I wonder if the compiler is smart enough to emit those instructions with the right optimization flags
I never saw a compiler generating these instructions on its own. Compilers do produce popcnt and bswap from recognized algorithms, so I guess it's not entirely unreasonable, but I think the benefit would be marginal.