10 ms·
One thing to consider is that the compiler can't simply replace a division by just a right shift for signed variables (it will round towards -inf for negative n
by mras0 4y ago
One thing to consider is that the compiler can't simply replace a division by just a right shift for signed variables (it will round towards -inf for negative numbers), so even today there's a tiny bit of benefit of explicitly using shifts if you know that the number can't be negative (and the compiler can't prove it) or you don't care about the rounding (https://godbolt.org/z/vTzYYxqz9 https://godbolt.org/z/vTzYYxqz9).
Of course that tiny bit of extra work is usually negligible, but might explain why the idiom has stuck around longer than you might otherwise expect.
- benj111 4y agoIf the number can't be negative surely you should be using unsigned ints?