Is multiplication and division using shift operators in C actually faster?

Short answer: Not likely.

Long answer:
Your compiler has an optimizer in it that knows how to multiply as quickly as your target processor architecture is capable. Your best bet is to tell the compiler your intent clearly (i.e. i*2 rather than i << 1) and let it decide what the fastest assembly/machine code sequence is. It’s even possible that the processor itself has implemented the multiply instruction as a sequence of shifts & adds in microcode.

Bottom line–don’t spend a lot of time worrying about this. If you mean to shift, shift. If you mean to multiply, multiply. Do what is semantically clearest–your coworkers will thank you later. Or, more likely, curse you later if you do otherwise.

Leave a Comment

tech