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

  • SEARCH
  • Home
  • 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 4112770
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:14:17+00:00 2026-05-20T22:14:17+00:00

There is a method in Java that reverses bits in an Integer reverseBytes(). I

  • 0

There is a method in Java that reverses bits in an Integer reverseBytes(). I wanted to try another implementation and this is what I have:

public static int reverse(int num) {

        int num_rev = 0;
        for (int i = 0; i < Integer.SIZE; i++) {
            System.out.print((num >> i) & 1);

            if (((num >> i) & 1)!=0) {

                num_rev = num_rev | (int)Math.pow(2, Integer.SIZE-i);
            }

        }
        return num_rev;
}

The result num_rev is not correct. Does anyone have any idea how to “reconstruct” the value? Maybe there is a better way to perform it?

Thanks for any suggestions.

  • 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-20T22:14:18+00:00Added an answer on May 20, 2026 at 10:14 pm

    The normal way would to reverse bits would be via bit manipulation, and certainly not via floating point math routines!

    e.g (nb: untested).

    int reverse(int x) {
        int y = 0;
        for (int i = 0; i < 32; ++i) {
            y <<= 1;       // make space
            y |= (x & 1);  // copy LSB of X into Y
            x >>>= 1;      // shift X right
        }
        return y;
    }
    

    Because x is right shifted and y left shifted the result is that the original LSB of x eventually becomes the MSB of y.

    A nice (and reasonably well known) method is this:

    unsigned int reverse(unsigned int x)
    {
        x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
        x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
        x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
        x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
        return ((x >> 16) | (x << 16));
    }
    

    This is actually C code, but as Java doesn’t have unsigned types to port to Java all you should need to do is remove the unsigned qualifiers and use >>> instead of >> to ensure that you don’t get any “sign extension”.

    It works by first swapping every other bit, then every other pair of bits, then every other nybble, then every other byte, and then finally the top and bottom 16-bit words. This actually works 🙂

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

Sidebar

Related Questions

Is there a method/function in Java that checks if another method/function is available just
I have a Java method that repeatedly evaluates the following expression in a very
Is there a standard class in Java that has a method to HTML-escape a
I have this java simulator that will need to handle a huge amount of
I have a Java String like this: peque\u00f1o. Note that it has an embedded
Is there any default method in Java that can count total occurrence of a
Is there a way to override a Java method that the only thing that
I have a small Java method that inserts short messages to a MySQL Database.
I'm interested to know if there already exists an approach that takes java method
I need a Regex that will match a java method declaration. I have come

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.