Seems like this has been asked before.
However, when I feel the need for bit-twiddling, I reach for my copy of the incomparable Hackers Delight. As it turns out, it contains discussions on finding the longest string of 1 bits, including a "logarithmic" implementation that could be used here on the bit/flipped (not) input:
int fmaxstr0(unsigned x, int *apos) { // invert bits. x = ~x; unsigned x2, x4, x8, x16, y, t; int s; if (x == 0) {*apos = 32; return 0;} x2 = x & (x << 1); if (x2 == 0) {s = 1; y = x; goto L1;} x4 = x2 & (x2 << 2); if (x4 == 0) {s = 2; y = x2; goto L2;} x8 = x4 & (x4 << 4); if (x8 == 0) {s = 4; y = x4; goto L4;} x16 = x8 & (x8 << 8); if (x16 == 0) {s = 8; y = x8; goto L8;} if (x == 0xFFFFFFFF) {*apos = 0; return 32;} s = 16; y = x16; L16: t = y & (x8 << s); if (t != 0) {s = s + 8; y = t;} L8: t = y & (x4 << s); if (t != 0) {s = s + 4; y = t;} L4: t = y & (x2 << s); if (t != 0) {s = s + 2; y = t;} L2: t = y & (x << s); if (t != 0) {s = s + 1; y = t;} L1: *apos = nlz(y); return s; }
Have fun!