Go4Expert (http://www.go4expert.com/)
-   Programming (http://www.go4expert.com/articles/programming/)
-   -   Using shift operator for faster division and multiplication (http://www.go4expert.com/articles/using-shift-operator-faster-division-t3095/)

Using shift operator for faster division and multiplication

Multiplications

12 * 2 = 12 << 1
12 * 4 = 12 << 2
12 * 8 = 12 << 3
12 * 16 = 12 << 4
12 * 32 = 12 << 5
12 * 64 = 12 << 6
12 * 128 = 12 << 7
12 * 256 = 12 << 8

Divisions

12 / 2 = 12 >> 1
12 / 4 = 12 >> 2
12 / 8 = 12 >> 3
12 / 16 = 12 >> 4
12 / 32 = 12 >> 5
12 / 64 = 12 >> 6
12 / 128 = 12 >> 7
12 / 256 = 12 >> 8

Code: C

`#include<stdio.h>  void main(void) {   int a;    a = 12 << 1;    printf("%d",a); }`

 mayjune 9Jul2009 02:55

Re: Using shift operator for faster division and multiplication

in machine terms,
for i12 / 256, 12 >> 8 is faster. Is that what you mean?
or just for easier calculations?

 teacher 17Apr2011 23:03

Re: Using shift operator for faster division and multiplication

this method does not work if you have logic one at msb
for eg
Code: cpp

`// before: binary equivalent 10000000 uint_8 t = 128;t<<1;// after: decimal equivalent : 0// after: binary equivalent 00000000  `

 All times are GMT +5.5. The time now is 07:09.