human
Count total set bits in all numbers from range L to R | Function that counts the set bits from 0 to N.
To store sum of set bits from 0 - N.
Until n >= to 2 ^ i.
This k will get flipped after 2 ^ i iterations.
Change is iterator from 2 ^ i to 1.
This will loop from 0 to n for every bit position.
When change = 1 flip the bit.
Again set change to 2 ^ i.
Increment the position.
Function that counts the set bit in the range ( L , R ).
Return the count.
Driver Code.
Given L and R.
Function Call"
Solution in Python:
assistant
#Here is code to count total set bits in all numbers from range L to R and the other steps required.
 def countSetBit ( n ) :
	i = 0 ;
	ans = 0 ;
	while ( ( 1 << i ) <= n ) :
		k = True ;
		change = 1 << i ;
		for j in range ( n + 1 ) :
			ans += 0 if k == True else 1 ;
			if ( change == 1 ) :
				k = False if k == True else True ;
				change = 1 << i ;
			else :
				change -= 1 ;
		i += 1 ;
	return ans ;
def countSetBits ( L , R ) :
	return abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ;
if __name__ == ' _ _ main _ _ ' :
	L = 3 ;
	R = 5 ;
	print ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " , countSetBits ( L , R ) ) ;