Hacker News new | ask | show | jobs
by barrkel 5426 days ago
Using bitwise and to implement modulo for a power of two is common as dirt in C; I'd object to it being considered a readability issue. Arguments about specific compilers in specific modes optimizing equivalent code to it using the % operator are beside the point. Often the whole point of using a power of two range is so that you can use a bitwise and at some point, and if it ultimately ends up in a library, it's likely that the argument to the and won't be constant (defeating the optimization). There are other considerations; at some point, the data type being wrapped around with the modulo may end up in a bitfield someplace (more likely in networking or tight storage situations). Being explicit about how many bits it uses doesn't hurt in that case.