Skip to main content
13 events
when toggle format what by license comment
Jun 17, 2020 at 9:04 history edited CommunityBot
Commonmark migration
Mar 14, 2018 at 15:05 comment added PrincePolka Nice, 3 bytes shorter than n=__builtin_popcount(x);
Jul 1, 2015 at 7:39 comment added Level River St @nmxprime the OP asks for unsigned int. for -7 = 11111111 11111111 11111111 11111001 on my 32 bit compiler, I get 30 for the fast algorithm, which is correct. For the naive algorithm, it iterates through -7, -7/2=-3, -3/2=-1, -1/2=0. That gives an incorrect answer. Changing x/=2 to x>>=1 may give the correct answer on some compilers, but C is undefined as to whether a 1 or a 0 is shifted into the empty bit for >> on negative numbers. Those compilers that shift a 1 in will go into an infinite loop. The workaround is to define x as an unsigned int. Then x=-7 loads (1<<32)-7=4294967289 into x.
Jul 1, 2015 at 5:21 comment added nmxprime The naive implmentation for(n=0;x;x/=2)n+=x&1; gives a result which is different from, for(n=0;x;n++)x&=x-1; for negative numbers. Try with x=-7, the results are confusing !
Apr 29, 2015 at 13:38 history edited Level River St CC BY-SA 3.0
edited body
Apr 29, 2015 at 3:58 comment added Helios Correction to naive algorithm: for(n=0;x;x/=2)n+=x&1;
Mar 23, 2015 at 18:35 comment added mbomb007 "First published in 1960", impressive.
Mar 18, 2015 at 4:39 vote accept ayane_m
Mar 18, 2015 at 4:40
Mar 17, 2015 at 8:43 history edited Level River St CC BY-SA 3.0
added 136 characters in body
Mar 17, 2015 at 8:33 comment added Level River St @edc65 so as it turns out, I reinvented the wheel. At least I saved 2 bytes by omitting the {}. It's such a simple task I shouldn´t be surprised someone already came up with it.
Mar 17, 2015 at 7:29 comment added edc65 Ref: graphics.stanford.edu/~seander/…
Mar 17, 2015 at 2:35 history edited Level River St CC BY-SA 3.0
added 37 characters in body
Mar 17, 2015 at 2:16 history answered Level River St CC BY-SA 3.0