Hi.
I'm very interested in binary representation of numbers.I see some attractive formula and methods such as :
(x)&(-x) return smallest significant of x that is 1 !
use binary for create all subsets of a small set :)
...
I want to learn more, so create this blog for sharing interest knowledges and making it worth post for competitive programmers. also I want to know is there a relation about remainder of numbers or divisibility of them in binary ? ( for example in decimal we know a number is divisible by 5 when it's last digit is 0 or 5 )
Count the number of ones in the binary representation of i.
oh interesting ... is there a proof for this formula?
not faster than
__builtin_popcount(i)
in MinGW and slower than__popcnt(i)
in MSVC.It means that in MinGW
__builtin_popcount
is not a single call toPOPCNT
instruction, but some kind of bit magic like your function. yet another MinGW issueHow did you test it?
ideone.com
And here in Codeforces Custom Test,
i < 1000000000
:n = 109 doesn't look like realistic constraint, and for 107...108 there is no meaningful difference. Anyway, MSVC is more than 2 times faster in this case.
Auto comment: topic has been updated by Frez (previous revision, new revision, compare).
You can find many tricks in Hacker's Delight book by Henry S. Warren.
https://graphics.stanford.edu//bithacks.html
This is really interesting!
Thank you for sharing this. Bookmarked.
thanks very beautiful!
A good article-sized read is the relevant TopCoder tutorial by bmerry.
For a more thorough study, the Hacker's Delight book mentioned before is the way to go.
thanks