What about:
var value = 529;
var count = 0;
var countMax = 0;
while(value > 0)
{
count = value % 2 == 0 ? count + 1 : 0;
value /= 2;
countMax = Math.Max(count, countMax);
}
// result in: countMax;
[Edit: Version that supports also negative ints]
var value = 529;
var count = 0;
var countMax = 0;
var factor = value < 0 ? -1 : 1;
while((value * factor) > 0)
{
count = value % 2 == 0 ? count + 1 : 0;
value /= 2;
countMax = Math.Max(count, countMax);
}
// result in: countMax;
[Edit2: added explanation]
Converting the integer to a string of bits and count the linked zeros using string operations is one option. However, it needs lots of code and is not realy performandreally performant.
Another option is to check if the number is even. If so, the last bit is not set ('0'). Dividing the number with 2 removes the last bit, so that we can check the next bit... and so on.