-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryGap.c
More file actions
36 lines (34 loc) · 837 Bytes
/
BinaryGap.c
File metadata and controls
36 lines (34 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int solution(int N)
{
// write your code in C99 (gcc 6.2.0)
unsigned int dividened_number = N;
int count_for_zero_length = 0;
int max_length = 0;
int flag_until_one = 0;
while (dividened_number != 0)
{
if (dividened_number & 0x1)
{
if (flag_until_one == 0)
{
flag_until_one = 1;
}
if (max_length < count_for_zero_length)
{
max_length = count_for_zero_length;
}
count_for_zero_length = 0;
}
else
{
if (flag_until_one == 0)
{
dividened_number >>= 1;
continue;
}
count_for_zero_length++;
}
dividened_number >>= 1;
}
return max_length;
}