<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Write a function
Step1:  n = 10
Step2:  n=100
Step3:  converting binary to decimal
Step4:  testing more binary to decimal conversions
<ASSISTANT_TASK:>
Python Code:
n = 1
print n.bit_length()
a = n.bit_length()
print bin(n)
print '%0*d' % (a, int(bin(n)[2:]))
print '{0:08b}'.format(n)
Explanation: Write a function:
def solution(N)
that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5.
Assume that:
    N is an integer within the range [1..2,147,483,647].
Complexity:
    expected worst-case time complexity is O(log(N));
    expected worst-case space complexity is O(1).
Analyzing number of bits
End of explanation
n = 10
print n.bit_length()
a = n.bit_length()
print bin(n)
print '%0*d' % (a, int(bin(n)[2:]))
print '{0:08b}'.format(n)
Explanation: n = 10
End of explanation
n = 10
print n.bit_length()
a = n.bit_length()
print bin(n)
print '%0*d' % (a, int(bin(n)[2:]))
print '{0:08b}'.format(n)
Explanation: n=100
End of explanation
n = 157
count_one = 0
count_gap = 0
binarygap = 0
for count in xrange(n.bit_length()):
    print count
    
    if n % 2:
        count_one +=1
        n = n /2
    elif count_one == 1:
        count_gap += 1
        print "count gap: ", count_gap
        n = n / 2
    
    if count_one > 1:
        if binarygap < count_gap:
            binarygap = count_gap
            print "binary gap", binarygap
        count_one = 1
        count_gap = 0
print binarygap
157 /2
78/2
Explanation: converting binary to decimal
End of explanation
n = 37
count_one = 0
count_gap = 0
binarygap = 0
for count in xrange(n.bit_length()):
    print count
    print "n", n
    
    if n % 2:
        count_one +=1
        n = n /2
    elif count_one == 1:
        count_gap += 1
        print "count gap: ", count_gap
        n = n / 2
    
    if count_one > 1:
        if binarygap < count_gap:
            binarygap = count_gap
            print "binary gap", binarygap
        count_one = 1
        count_gap = 0
print binarygap
n = 142
count_one = 0
count_gap = 0
binarygap = 0
for count in xrange(n.bit_length()):
    print count
    print "n", n
    
    if n % 2:
        count_one +=1
        n = n /2
    elif count_one == 1:
        count_gap += 1
        print "count gap: ", count_gap
        n = n / 2
    else:
        n = n/2
    
    if count_one > 1:
        if binarygap < count_gap:
            binarygap = count_gap
            print "binary gap", binarygap
        count_one = 1
        count_gap = 0
print binarygap
142 / 2
71/2
35/2
n = 488
count_one = 0
count_gap = 0
binarygap = 0
for count in xrange(n.bit_length()):
    print count
    print "n", n
    
    if n % 2:
        count_one +=1
        n = n /2
    elif count_one == 1:
        count_gap += 1
        print "count gap: ", count_gap
        n = n / 2
    else:
        n = n/2
    
    if count_one > 1:
        if binarygap < count_gap:
            binarygap = count_gap
            print "binary gap", binarygap
        count_one = 1
        count_gap = 0
print binarygap
n = 181
count_one = 0
count_gap = 0
binarygap = 0
for count in xrange(n.bit_length()):
    print count
    print "n", n
    
    if n % 2:
        count_one +=1
        n = n /2
    elif count_one == 1:
        count_gap += 1
        print "count gap: ", count_gap
        n = n / 2
    else:
        n = n/2
    
    if count_one > 1:
        if binarygap < count_gap:
            binarygap = count_gap
            print "binary gap", binarygap
        count_one = 1
        count_gap = 0
print binarygap
def solution(N):
    
    count_one = 0
    count_gap = 0
    binarygap = 0
    
    for count in xrange(N.bit_length()):   
        if N % 2:
            count_one +=1
            
            N = N /2
        
        elif count_one == 1:
            count_gap += 1
            
            N = N / 2
        
        else:
            N = N/2
        
        if count_one > 1:
            if binarygap < count_gap:
                binarygap = count_gap
                
            count_one = 1
            
            count_gap = 0
    
    return binarygap
print solution(181)
Explanation: testing more binary to decimal conversions
End of explanation
<END_TASK>