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 7852917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:32:10+00:00 2026-06-02T19:32:10+00:00

Question #1 In Java, is shifting multiple times more expensive than using a single

  • 0

Question #1

In Java, is shifting multiple times more expensive than using a single statement to shift the by the same number?

For example, is

int x = 5;
x = x << 16;

Faster than

int x = 5;
for (int i=0; i<16; ++i) {
    x = x << 1;
}

Further, what about

int x = 5;
for (int i=0; i<16; ++i) {
    x = x*2;
}

Edit: What is the precise performance of “x << 16”? Is it the same speed as “x << 1”?

Question #2

Is there a resource online that I can utilize to determine various bitwise operation performances in Java, so that I do not have to waste the time of StackOverflow users? 🙂

  • 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-06-02T19:32:11+00:00Added an answer on June 2, 2026 at 7:32 pm

    In terms of basic logic, a single shift would give much greater performance.

    When using the for loop version, for every iteration of the loop, the loop’s terminating condition is checked, i is incremented, a bitwise operation is carried out and an assignment is made to x.

    When using a single shift, a single bitwise operation is carried out and an assignment is made to x.

    And as others have said, this really does seem like premature optimisation.

    For the sake of answering your question though, logically, the first example is faster than the others.

    That said, depending on the language and compiler, it is possible that the compiler would see that your for loop always runs 16 times and then proceeds to optimise your code, changing it to x << 16. If this is the case, you would see no difference between each code example you gave.

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

Sidebar

Related Questions

I have a more complicated issue (than question 'Java map with values limited by
I had a question about Java Class Path variables. If I have multiple jars
I'm using VB.NET but this question applies to any OO language - Java, C#
This is more of a language design rather than a programming question. The following
This is a beginner question for java graphics using the awt package. I found
Possible Duplicates: Is shifting bits faster than multiplying and dividing in Java? .NET? Quick
Java Question: I want to take an undetermined number of lines of input from
Possible Duplicate: Design question regarding Java EE entity with multiple language support I'm working
How can you instantiate a Bimap of Google-collections? I've read the question Java: Instantiate
Naive question about Java syntax. What does <T> T accept(ObjectVisitorEx<T> visitor); mean? What would

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.