7 ms·
In http://www.techiedelight.com/count-set-bits-using-lookup-table/ http://www.techiedelight.com/count-set-bits-using-lookup-tab... , the solution does not appea
by eesmith 9y ago
In http://www.techiedelight.com/count-set-bits-using-lookup-table/ http://www.techiedelight.com/count-set-bits-using-lookup-tab... , the solution does not appear to be portable, that is, it uses the right shift of a signed number, and the C specification says (from http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1336.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1336.pdf )
> The result of E1 >> E2 is E1 right-shifted E2 bit positions. ... If E1 has a signed type and a negative value, the resulting value is implementation-defined.
In practice it isn't a concern, as the most common compilers (gcc, clang, MSVC++) do what we expect.
- tlb 9y agoYes, it's only potentially different on CPUs with non-twos-complement arithmetic, which nobody has made in a long time. I think the last was the PDP-6 in 1963. I'd prefer to have a C standard that specified everything you can count on in a modern implementation.