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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:27:59+00:00 2026-05-13T06:27:59+00:00

I have some C code that I’d like to port to java. I haven’t

  • 0

I have some C code that I’d like to port to java. I haven’t done much C coding, but I was able to follow along up until this one function. If anyone could help me understand what is going on, it would be greatly appreciated.

int reverse_integer(int input) {
    int output = 0, i;

    for ( i=0, i<sizeof(int); i++ ) {
         output = ( input & 0x000000FF ) | output; 
         input >>= 8;
         if ( i < 3 ) {
             output <<= 8;
         }
    }

    return output;
}

The function is used as such:

char * position = //some data

/*the included comment on this next line states its the size of a string*/
int i = reverse_integer( *(int*)position ) 
  • 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-13T06:27:59+00:00Added an answer on May 13, 2026 at 6:27 am

    I’ll be happy to post Java code that does the same as the C code, but only if you promise not to use it.

    /** Reverses the bytes in an integer. */
       public static int reverseInteger(int input) {
          return
             (input >>> 24) |
             (input >> 8) & 0x0000ff00 |
             (input << 8) & 0x00ff0000 |
             (input << 24);
       }

    Note that there’s no point in looping – an int in Java is always 4 bytes. Also note the triple-right angle bracket at the beginning the expression to perform an unsigned right-shift.

    Now for the reasons not to use it:

    1 – The function already exists – see Integer.reverseBytes(int)

    2 – You’re going to have a hard time using that example code, since Java doesn’t let you cast an array of bytes as anything else. Java is officially big-endian (most significant byte first), so if you’re reading bytes from a file then you can use java.io.DataInputStream to extract ints, longs, etc.

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

Sidebar

Related Questions

I have some code that looks like this: foreach(var obj in collection) { try
I have some code that is basically 5 print statements that i would like
I have some code that looks something like this: d = {'foo': True, 'bar':
I have some code that needs to be able to find the version number
I have some code that I want to execute if an exception happens. But
I have some code that looks something like this: <? $addr = array(city =>
I have some code that looks like my ($ids,$nIds); while (<myFile>){ chomp; $ids.= $_
I have some code that raises PropertyChanged events and I would like to be
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that I've inherited from someone very clever where they like

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.