Timeline for Counting the number of "on" bits in an int
Current License: CC BY-SA 3.0
7 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Nov 7, 2016 at 3:00 | history | edited | JS1 | CC BY-SA 3.0 |
added 659 characters in body
|
Nov 5, 2016 at 4:28 | comment | added | JS1 |
@DavidBowling If the input must be signed, then Just cast the input argument to an unsigned int. Like this: int on_bits(int n) { unsigned int num = (unsigned int) n; ...
|
|
Nov 5, 2016 at 2:53 | comment | added | ad absurdum |
@JS1-- I was just looking over your answer again, and it occurs to me that many of the possibilities that you mention may not work for this particular problem, as the input is an int , not an unsigned int . This could lead to problems, at least for the solutions involving right-shifts of the bits of the input number. Still, your answer was very informative and has given me plenty to think about.
|
|
Nov 5, 2016 at 2:33 | vote | accept | ad absurdum | ||
Nov 5, 2016 at 2:21 | comment | added | isanae | Gotta love bit fiddling on a Friday night. A most interesting read. | |
Nov 4, 2016 at 22:37 | comment | added | ad absurdum |
@JS1-- This is exactly what I was looking for. I can't believe that I missed the obvious and set b_mask in a loop instead of using a single bit-shifting operation! Thank you for all of the information.
|
|
Nov 4, 2016 at 22:14 | history | answered | JS1 | CC BY-SA 3.0 |