Hello I am new to C and was wondering if someone could just please explain to me what this function does? Thats all I just need help understanding it. I'm new to C so some one the syntax is tough for me to understand and Im not sure what the function does overall.
Thanks

#include <stdio.h>

    unsigned int mystery2(unsigned int n) {

      unsigned int temp = 
        (n >> 24) | 
        ((n << 8) & 0x00FF0000) |
        ((n >> 8) & 0x0000FF00) |
        (n << 24);

      return temp;
    }

    unsigned int prompt_and_read() {
      unsigned int next;
      printf("\nprompt> Please enter an integer (0 to exit): ");
      scanf("%u", &next);
      if (next == 0) exit(0);
      return next;
    }

    int main() {
      while (1) {
        unsigned int next = prompt_and_read();
        unsigned int result = mystery(next);
        printf("%d ===> %d\n", next, result);
      }
      return 0;
    }

Following the logic of this program: the main method calls "prompt_and_read();", this method simply takes in an unsigned integer and returns it to the main method. From there the "mystery2();" method is called. This is what I am assuming you are asking about. What is going in this method are called bitwise operations.

Simply search on Google for "Bitwise Operations in C" or on Daniweb and you will find plenty of explanations as to what they are.

Hope that helps.

Ya it did, I did a little more research, this supposubly reverses the byte order of n changing the values between little and big endian?

Looks correct, provided unsigned int is 32 bits (of course it could be 16 bits or 64 bits also). I would at least use unsigned long, which is at least 32 bits, and check sizeof (unsigned long), the swap operation is length dependent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.