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="James Harris, post: 3126667"] Since others are going for speed how about the following. (The C may not be completely correct.) It combines fast lookup with a small table. There is a reasonable chance that the looked up value will be in cache (which would be better if there were some way to align the array). int nibble_bits[] = { /* The number of bits in each nibble 0 to 15 */ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 } int bitcount (int i) { int bits_set = 0; for (; i; i >>= 4) { bits_set += nibble_bits[i & 0xf]; } return bits_set; } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Bit count of an integer
Top