human
Can you provide a code to occurrences of a pattern in binary representation of a number and the other steps required given the following:
Occurrences of a pattern in binary representation of a number | Function to return the count of occurrence of pat in binary representation of n.
To store decimal value of the pattern.
To store a number that has all ones in its binary representation and length of ones equal to length of the pattern.
Find values of pattern_int and all_ones.
If the pattern occurs in the last digits of n.
Right shift n by 1 bit.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to occurrences of a pattern in binary representation of a number and the other steps required.
 def countPattern ( n , pat ) :
	pattern_int = 0
	power_two = 1
	all_ones = 0
	i = len ( pat ) - 1
	while ( i >= 0 ) :
		current_bit = ord ( pat [ i ] ) - ord ( '0' )
		pattern_int += ( power_two * current_bit )
		all_ones = all_ones + power_two
		power_two = power_two * 2
		i -= 1
	count = 0
	while ( n != 0 and n >= pattern_int ) :
		if ( ( n & all_ones ) == pattern_int ) :
			count += 1
		n = n >> 1
	return count
if __name__ == ' _ _ main _ _ ' :
	n = 500
	pat = "10"
	print ( countPattern ( n , pat ) )