Bit Shifting... Yay or Nay? :)

Started by
9 comments, last by GameDev.net 24 years, 3 months ago
Hands down, a single bit shift operation will beat a multiplication by a power of two. Your compiler will know this and change any multiplications by a constant power of two into a bit shift. The real question is if a mulitplication of a number that is a sum of two or more powers of two will beat out the bit shift/addition combo. (i*6 faster than (i << 2) + (i << 1) ?) On the 486 processors and earlier shift/add was faster for the some of two powers of two, but I haven't benched it since then, but again with the newer processors the superscalar architecture should make sure that even if you are doing a "slow" multiplication, no processors cycles are wasted.

Having the processor perform the bit shifts would also be faster than an array lookup. In the array lookup, you have to access memory possible multiple times with the chance of cache miss, page fault, etc.

This topic is closed to new replies.

Advertisement