public static int solution(int N) {
int binaryGap = 0;
// Special case
if (N == 0) {
return 0;
}
// remove trailing zeroes if not counted; credit to Peter Taylor
while (N % 2 == 0) {
N /= 2;
}
for (int j = 0; N > 0; N /= 2) {
if (N % 2 == 0) {
j++;
} else {
if (j > binaryGap) {
binaryGap = j;
}
j = 0;
}
}
return binaryGap;
}
the small detail was added for the case when N=0