Which is faster: x<<1 or x<<10?

前端 未结 9 2086
感情败类
感情败类 2020-12-13 03:32

I don\'t want to optimize anything, I swear, I just want to ask this question out of curiosity. I know that on most hardware there\'s an assembly command of bit-shift (e.g.

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 03:47

    Some embedded processors only have a "shift-by-one" instruction. On such processors, the compiler would change x << 3 into ((x << 1) << 1) << 1.

    I think the Motorola MC68HCxx was one of the more popular families with this limitation. Fortunately, such architectures are now quite rare, most now include a barrel shifter with a variable shift size.

    The Intel 8051, which has many modern derivatives, also cannot shift an arbitrary number of bits.

提交回复
热议问题