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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:30:00+00:00 2026-05-17T01:30:00+00:00

I was helping someone with their homework and ran into this strange issue. The

  • 0

I was helping someone with their homework and ran into this strange issue. The problem is to write a function that reverses the order of bytes of a signed integer(That’s how the function was specified anyway), and this is the solution I came up with:

int reverse(int x)
{
    int reversed = 0;

    reversed = (x & (0xFF << 24)) >> 24;
    reversed |= (x & (0xFF << 16)) >> 8;
    reversed |= (x & (0xFF << 8)) << 8;
    reversed |= (x & 0xFF) << 24;

    return reversed;
}

If you pass 0xFF000000 to this function, the first assignment will result in 0xFFFFFFFF. I don’t really understand what is going on, but I know it has something to do with conversions back and forth between signed and unsigned, or something like that.

If I either append ul to 0xFF it works fine, which I assume is because it’s forced to unsigned then converted to signed or something in that direction. The resulting code also changes; without the ul specifier it uses sar(shift arithmetic right), but as unsigned it uses shr as intended.

I would really appreciate it if someone could shed some light on this for me. I’m supposed to know this stuff, and I thought I did, but I’m really not sure what’s going on here.

Thanks in advance!

  • 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-17T01:30:00+00:00Added an answer on May 17, 2026 at 1:30 am

    Since x is a signed quantity, the result of (x & (0xFF << 24)) is 0xFF000000 which is also signed and thus a negative number since the top (sign) bit is set. The >> operator on int (a signed value) performs sign extension (Edit: though this behaviour is undefined and implementation-specific) and propagates the sign bit value of 1 as the value is shifted to the right.

    You should rewrite the function as follows to work exclusively on unsigned values:

    unsigned reverse(unsigned x)
    {
        unsigned int reversed = 0;
    
        reversed = (x & (0xFF << 24)) >> 24;
        reversed |= (x & (0xFF << 16)) >> 8;
        reversed |= (x & (0xFF << 8)) << 8;
        reversed |= (x & 0xFF) << 24;
    
        return reversed;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm helping someone diagnose some slowness with their RoR applications. I'm not that deep
I'm helping a colleague with a recurring issue that's just started happening to multiple
THANKS FOR HELPING!!! Ok... So I am trying to write a script that will
Ok, so I am helping someone with their site. Their original coder thought it
I am a java/php developer helping someone with actionscript. I don't understand why this
Is someone able to assist in helping understand how to get this C# example
Today while helping someone I came across an interesting issue which I couldn't understand
I'm not familiar with ASP, but am helping someone out with their website as
Am helping someone to install WP on their current host, which unfortunately only has
I'm helping someone that needs to send an encrypted value with AES to a

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.