Quantcast
Channel: Bits counting algorithm (Brian Kernighan) in an integer time complexity - Stack Overflow
Browsing all 6 articles
Browse latest View live

Answer by Łukasz Zephyr for Bits counting algorithm (Brian Kernighan) in an...

This question is really about meaning of N in big O notation, not complexity of algorithm.N means size of the data. But in case where where "data" is a single number you need to define what you...

View Article


Answer by PengOne for Bits counting algorithm (Brian Kernighan) in an integer...

This algorithm goes through as many iterations as there are set bits. So if we have a 32-bit word with only the high bit set, then it will only go once through the loop. In the worst case, it will pass...

View Article


Answer by The_Sympathizer for Bits counting algorithm (Brian Kernighan) in an...

There are floor(lg(N)) + 1 significant bits in N -- that's a base-2 logarithm. The number of 1 bits in n is at most this. So the time will have asymptotic upper bound O(lg(N)) = O(log(N)).

View Article

Bits counting algorithm (Brian Kernighan) in an integer time complexity

Can someone explains why Brian Kernighan's algorithm takes O(log N) to count set bits (1s) in an integer. A simple implementation of this algorithm is below (in JAVA)int count_set_bits(int n){ int...

View Article

Answer by Uri London for Bits counting algorithm (Brian Kernighan) in an...

I would like to add to this discussion, a true O(Log N) algorithm, where N is the number of bits (32 or 64):int HammingWeight(DWORD dw) { dw = (dw & 0x55555555) + ((dw & 0xaaaaaaaa) >>...

View Article


Answer by wiesiu_p for Bits counting algorithm (Brian Kernighan) in an...

This is the matter of number representation only.I normal life we use decimal number system which allows us to write any number with digits 0-9. It allow us to write N with log(N) with base 10 number...

View Article
Browsing all 6 articles
Browse latest View live


Latest Images