Wednesday 28 February 2007

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

Example


<?php
$a = 12 << 1;
print $a;
?>



Community discussing C and its derivatives like Win32, C++, MFC, C# tutorials - www.cfanatic.com

1 comment:

Dainty Damsel said...

Hey that was good and basic funda.
Cursorily went through your blogs. You must have real patience to mainatin so many.

Well you can visit my quiz blog[Tech Quiz] at www.enigmaticexpressions.blogspot.com

cheers