#C 35 bytes
C 35 bytes
N;f(n){for(N=n;n&n-1;)n&=n-1;N-=n;}
n&=n-1
Does the opposite of the problem statement, it clears the least significant bit.
I have a vague memory of there being a similar trick for most significant bit from seeing it on coding game but may remember wrong.
Recursive function, (削除) 45 (削除ここまで) 43 bytes
N;g(n){n&=(N=n&~-n)?g(N):n;}f(n){N=n-g(n);}
cleblancs code shortened to 34 bytes
i=1;f(n){for(;n/i/2;i*=2);n-=n^i;}
28 bytes, possibly cheating , using pointer instead of return
f(*n){*n^=1<<(int)log2(*n);}
Try it on ideone! , doesn't work on tio.run
#C 35 bytes
N;f(n){for(N=n;n&n-1;)n&=n-1;N-=n;}
n&=n-1
Does the opposite of the problem statement, it clears the least significant bit.
I have a vague memory of there being a similar trick for most significant bit from seeing it on coding game but may remember wrong.
Recursive function, (削除) 45 (削除ここまで) 43 bytes
N;g(n){n&=(N=n&~-n)?g(N):n;}f(n){N=n-g(n);}
cleblancs code shortened to 34 bytes
i=1;f(n){for(;n/i/2;i*=2);n-=n^i;}
28 bytes, possibly cheating , using pointer instead of return
f(*n){*n^=1<<(int)log2(*n);}
Try it on ideone! , doesn't work on tio.run
C 35 bytes
N;f(n){for(N=n;n&n-1;)n&=n-1;N-=n;}
n&=n-1
Does the opposite of the problem statement, it clears the least significant bit.
I have a vague memory of there being a similar trick for most significant bit from seeing it on coding game but may remember wrong.
Recursive function, (削除) 45 (削除ここまで) 43 bytes
N;g(n){n&=(N=n&~-n)?g(N):n;}f(n){N=n-g(n);}
cleblancs code shortened to 34 bytes
i=1;f(n){for(;n/i/2;i*=2);n-=n^i;}
28 bytes, possibly cheating , using pointer instead of return
f(*n){*n^=1<<(int)log2(*n);}
Try it on ideone! , doesn't work on tio.run
#C 35 bytes
N;f(n){for(N=n;n&n-1;)n&=n-1;N-=n;}
n&=n-1
Does the opposite of the problem statement, it clears the least significant bit.
I have a vague memory of there being a similar trick for most significant bit from seeing it on coding game but may remember wrong.
Recursive function, (削除) 45 (削除ここまで) 43 bytes
N;g(n){n&=(N=n&~-n)?g(N):n;}f(n){N=n-g(n);}
cleblancs code shortened to 34 bytes
i=1;f(n){for(;n/i/2;i*=2);n-=n^i;}
28 bytes, possibly cheating , using pointer instead of return
f(*n){*n^=1<<(int)log2(*n);}
Try it on ideone! , doesn't work on tio.run
#C 35 bytes
N;f(n){for(N=n;n&n-1;)n&=n-1;N-=n;}
n&=n-1
Does the opposite of the problem statement, it clears the least significant bit.
I have a vague memory of there being a similar trick for most significant bit from seeing it on coding game but may remember wrong.
Recursive function, (削除) 45 (削除ここまで) 43 bytes
N;g(n){n&=(N=n&~-n)?g(N):n;}f(n){N=n-g(n);}
cleblancs code shortened to 34 bytes
i=1;f(n){for(;n/i/2;i*=2);n-=n^i;}
#C 35 bytes
N;f(n){for(N=n;n&n-1;)n&=n-1;N-=n;}
n&=n-1
Does the opposite of the problem statement, it clears the least significant bit.
I have a vague memory of there being a similar trick for most significant bit from seeing it on coding game but may remember wrong.
Recursive function, (削除) 45 (削除ここまで) 43 bytes
N;g(n){n&=(N=n&~-n)?g(N):n;}f(n){N=n-g(n);}
cleblancs code shortened to 34 bytes
i=1;f(n){for(;n/i/2;i*=2);n-=n^i;}
28 bytes, possibly cheating , using pointer instead of return
f(*n){*n^=1<<(int)log2(*n);}
Try it on ideone! , doesn't work on tio.run
#C 35 bytes
N;f(n){for(N=n;n&n-1;)n&=n-1;N-=n;}
n&=n-1
Does the opposite of the problem statement, it clears the least significant bit.
I have a vague memory of there being a similar trick for most significant bit from seeing it on coding game but may remember wrong.
Recursive function, 45(削除) 45 (削除ここまで) 43 bytes
N;g(n){n=n&n&=(N=n&~-n)?g(N):n;}f(n){nN=n-=n^gg(n);}
cleblancs code shortened to 34 bytes
i=1;f(n){for(;n/i/2;i*=2);n-=n^i;}
#C 35 bytes
N;f(n){for(N=n;n&n-1;)n&=n-1;N-=n;}
n&=n-1
Does the opposite of the problem statement, it clears the least significant bit.
I have a vague memory of there being a similar trick for most significant bit from seeing it on coding game but may remember wrong.
Recursive function, 45 bytes
N;g(n){n=n&(N=n&~-n)?g(N):n;}f(n){n-=n^g(n);}
cleblancs code shortened to 34 bytes
i=1;f(n){for(;n/i/2;i*=2);n-=n^i;}
#C 35 bytes
N;f(n){for(N=n;n&n-1;)n&=n-1;N-=n;}
n&=n-1
Does the opposite of the problem statement, it clears the least significant bit.
I have a vague memory of there being a similar trick for most significant bit from seeing it on coding game but may remember wrong.
Recursive function, (削除) 45 (削除ここまで) 43 bytes
N;g(n){n&=(N=n&~-n)?g(N):n;}f(n){N=n-g(n);}
cleblancs code shortened to 34 bytes
i=1;f(n){for(;n/i/2;i*=2);n-=n^i;}