I need a fast way to count the number of bits in an integer in python.
My current solutions is
bin(n).count("1")
but I am wondering if there is any faster way of doing this?
PS: (i am representing a big 2D binary array as a singe list of numbers and doing bitwise operations, and that brings the time down from hours to minutes. and now i would like to get rid of those extra minutes.
Edit:
1. it has to be in python 2.7 or 2.6
and optimizing for small numbers does not matter that much since that would not be a clear bottle neck, but I do have numbers with 10 000 + bits at some places
for example this is a 2000 bit case:
124480579411363943422977485485450829978158403576349485507396127987323099759232806852458769500556143622837697107058111829761428033242424070171048410620648401132628401376255826466830689041492965010297546