Tip of the week#5: Fast multiplication

It is well known that bit shifting enables programmers to do multiplication when dealing with numbers such as 2, 4, 8, 16, 32, 2^n, …

For instance:
a=a<<4;
equals
a*=16;

But multiplying by 100 (and for many other numbers) is also possible:
a*=100;
equals
a= a*64 + a*32 + a*4;
equals
a=(a<<6)+(a<<5)+(a<<2);

Some compilers might optimise this by themselves.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet