Hacker News new | ask | show | jobs
by Scotchy 5117 days ago
I often find myself using 1 << Y when I mean 2^Y, but just because it is faster to write and generally accepted in all languages than to find pow in the current language I am writing in...

Edit: fixed

2 comments

You're computing 2^Y there.

pow is usually a floating point operation, that's why it's slow. Modern CPU's may actually be fast at a pow floating point operation, but it's still expensive to move integers from general purpose registers to floating point registers.

You generally calculate 2^Y because you want to work with a Y-bit quantity somehow. Like generating a mask with the Yth bit set, calculating the size of a Y-bit address space, allocating storage for a power set of Y items, ... In these cases it's OK because bits are actually the thing that matters. With division, they usually aren't.