Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6789947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:39:59+00:00 2026-05-26T17:39:59+00:00

I recently encountered the following interview question: How can you multiply a number by

  • 0

I recently encountered the following interview question:

How can you multiply a number by 7 in an efficient and optimized way?

I know that I can multiply by 8 (or left-shift by three bits) and then subtract the original value:

num = (num << 3) - num;

but are there any other solutions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:39:59+00:00Added an answer on May 26, 2026 at 5:39 pm

    To get a multiple of 7 in an efficient way:

    7
    

    7 is a multiple of 7. That answers the question you asked, but I’m sure it doesn’t answer the question you mean to ask.

    EDIT: The above is based on the question’s original title,which I’ve just corrected.

    To multiply by 7 efficiently, just write, for example:

    x * 7
    

    and invoke your compiler with optimization. Let the compiler figure out whether a single MUL instruction or something like (x<<3) - x is more efficient for the current machine.

    There’s yet another implicit question here: what answer was the interviewer looking for? I hope that “let the compiler worry about it” would be an acceptable answer. (x<<3) - x is probably the most obvious micro-optimization — but it can yield incorrect answers if x<<3 overflows, and depending on the system it might be slower than a MUL instruction.

    (If I were the interviewer, I’d be more impressed by a good explanation and understanding of the issues than by any specific answer.)

    EDIT

    On further thought, the kinds of micro-optimizations that have been discussed here might be useful if you know more about the possible values of x than the compiler does. If you know, because of the nature of your program’s logic, that x will always be in the range 0..10, then a lookup table could easily be faster than a multiply operation. Or if you know that x is in that range 99% of the time, a lookup table with a fallback to an actual multiplication might be just the thing.

    But if the compiler’s analysis of your program flow doesn’t allow it to prove that x is always in that range, then it can’t perform this kind of optimization.

    But such circumstances are very rare. And when your code runs in a new environment where x can be 11 (perhaps it’s running on a device with a larger display), kaboom. And the performance improvement very likely wasn’t significant in the first place.

    There are times when micro-optimization is appropriate, but there is a substantial cost in development and testing time. Do it only if actual measurements indicate that it’s worth it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently encountered a situation in some code I am working on that doesn't
I recently encountered an index in a database I maintain that was of the
I recently encountered with this question: How to reduce this expression: s>73?61:60; . The
I've recently encountered the following exception... java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already
I've recently encountered a problem with OpenX. I can no longer choose zones to
I recently encountered some code that gcc would not compile without this arg. I
I've recently encountered a very rarely reproducible bug. If I cast a pointer that
I was given this question at a job interview recently and couldn't figure out
I've recently discovered that the ON clause of a LEFT JOIN may contain values
I've recently encountered the following problem with my application: it didn't show any console

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.