The link posted by Qnan shows an elegant solution to the general case.
For particular values of m it could be further optimized.
For instance, for m == 4, you can just do:
x &= (x >> 1); x &= (x >> 2); // at this point, the first bit set in x indicates a 4 bit set sequence. For m == 6 :
x &= (x >> 1); x &= (x >> 1); x &= (x >> 3); In the end, that just reduces to factoring m.
update
Note also, that for high values of, it may actually be cheaper to just check for the bit sequence at every possible position.
For instance, for m = 23, the pattern can only start at positions from 0 to 9.