3 ms·
I don't understand all of the code but this bit > int valbit = Possibles & (-Possibles); // Lowest 1 bit in Possibles is pretty neat
by yread 5y ago
I don't understand all of the code but this bit
> int valbit = Possibles & (-Possibles); // Lowest 1 bit in Possibles
is pretty neat
- toxik 5y agoI think there’s an Intel intrinsic for this, but nonetheless neat.
- Cthulhu_ 5y agoIt's the kind of thing that I would expect a compiler to know about and use; when writing code in a high level language you shouldn't have to think too much about what happens on the hardware level.
- xxs 5y agoIt's done in Java, Integer.lowestOneBit(int), citing HD 2-1. Java defines ints to be 2-complimentary. I can't recall if it's compiled tzcnt[0], though [0]: https://www.felixcloutier.com/x86/tzcnt https://www.felixcloutier.com/x86/tzcnt
- hyperbovine 5y agoIt's a standard trick, e.g. https://stackoverflow.com/questions/12247186/find-the-lowest-set-bit https://stackoverflow.com/questions/12247186/find-the-lowest....
- jlebar 5y agoIf you like that you may also like https://graphics.stanford.edu/~seander/bithacks.html https://graphics.stanford.edu/~seander/bithacks.html (Also "Hackers Delight" if you really can't get enough of it.)