This site features a collection of common technical interview questions gathered by a group of programmers who have been through, and given, lots of technical interviews. There is an emphasis on C++ and game programming technical interviews, but most of the questions are relevant to any technical interview.
Lucky Number Seven

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

int MultBySeven(int iA)
{
int result = 0;
for(int i = 0; i

Fastest does not use loops!
7x = x + 2x + 4x
or
7x = x + x

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…



Leave a comment
If you are including code in your comment, place it within a <div class='code'></div> tag for better formatting.


Do you have a technical interview question you would like to submit? Some tips you would like to pass on? Just want to say hi? Feel free to contact us