Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Bit count of an integer
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="pete, post: 3126508"] (i) is the wrong type. It should be unsigned. If (i) has a value of (-1), then how many bits are set in (i) depends on which of three representations is used, but bitcount will always return the number of bits used by two's complement representation. The last time that I was looking cpu's was in the 1990's. At that time, typically, a conditional jump took about twice as long as an integer addition operation. By every metric I know for guessing how fast a function will be, bit_count should be faster than bitcount. unsigned bit_count(unsigned n) { unsigned count; for (count = 0; n != 0; n &= n - 1) { ++count; } return count; } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Bit count of an integer
Top