Write a fast routine for multiplying an unsigned integer by 7. Then write a fast routine for dividing an unsigned integer by 7.
Write a fast routine for multiplying an unsigned integer by 7. Then write a fast routine for dividing an unsigned integer by 7.
4 Comments so far
Leave a comment
To Multiply
int MultBySeven(int iA)
{
int result = 0;
for(int i = 0; i
By Sundar on 07.13.07 4:23 am | Permalink
int MultBySeven(int iA)
{
int result = 0;
for(int i = 0; i
By Sundar on 07.13.07 4:23 am | Permalink
Fastest does not use loops!
7x = x + 2x + 4x
or
7x = x + x
By Rodneyk on 02.04.08 12:47 pm | Permalink
Fastest does not use loops!
This time with out the < inline
7x = x + 2x + 4x
or
7x = x + x<<1 + x<<2;
int MulBySeven(int x)
{
int x2 = x<<1;
int x4 = x2<<1;
return x + x2 + x4;
}
if you did not want to use the shift you get:
int MulBySeven(int x)
{
int x2 = x+x;
int x4 = x2+x2;
return x + x2 + x4;
}
This still is reduced the number of math operations to 4 (down from 7 for the loop case) and it comes with NO looping overhead…
By Rodneyk on 02.04.08 12:51 pm | Permalink
Leave a comment
If you are including code in your comment, place it within a <div class='code'></div> tag for better formatting.