Reverse code understanding

Hi,
Could anyone explain me which value change after call reverse function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;

static  void  reverse (int len, char *buf)
{
  char  tmp[24];
  int  i;

  for (i = 0; i < len; i++) tmp[i] = buf[i];
  for (i = 0; i < len; i++) buf[i] = tmp[ (len - 1) - i];
}
inline  void  reverse (int len, unsigned char *s)  { cout<<"unsigned char "<<endl;reverse (len, (char *) s); }
inline  void  reverse (int len, short *s)  { reverse (len, (char *) s); }
inline  void  reverse (int len, int *s)    { cout<<"int "<<endl;reverse (len, (char *) s); }

int main()
{
    int data = 1485;
    reverse(4,&data);
    cout<<data;
    return 0;
}
Last edited on
First off, that's a non-portable call to reverse. You're assuming that an int is always 4 bytes. As was explained in this thread, that's not always the case.
http://www.cplusplus.com/forum/general/105712/

A better call would have been:
 
reverse(sizeof(int),&data);


Assuming you're on a 32 bit platform, data contains 0x000005cd.
Reversing the order of the bytes results in 0xcd050000.
I am bit confuse on the implementation of reverse function.How is it reverse the code.I mean When I pass 1485. I was expecting 5841. But I know it is not the case with above code. it accept char pointer and access up to int
Last edited on
It reverses the bytes of the variable. If you write the number in hexadecimal it is more obvious because 2 hex digits is 1 byte.

1
2
3
4
5
6
7
int main()
{
    int data = 0x10203040;
    reverse(4,&data);
    cout << hex << data;
    return 0;
}
int
40302010
Last edited on
You declared 1485 as an int. An int is stored as binary.
You're expecting the decimal digits to be reversed. That would only happen if 1485 were stored as character data.

1
2
char data[4] = {'1', '4', '8', '5' };
reverse(4,&data); 


Thanks for reply.I have tried to print tmp in the reverse fucntion but it did not print anything.
Any point.
Last edited on
If you print tmp you will print it as if it was a C string, which is not the case. tmp might contain characters that can't be displayed properly and there might not be a null character to mark the end of the string.
Thanks. is there any difference for above code in 32 bit hex decimal reverse to 64 bit hex decimal value
hexadecimal has nothing to do with it. Data is stored as binary bits. Hex is simply a way of grouping those bits for easier representation.

The reverse function above works by reversing the order of some number of bytes (up to a limit of 24). How many bytes you reverse (and whether you need to at all), depends on the number representation on the source and target platforms.

You really need to spend some time studying number representation.
http://en.wikipedia.org/wiki/Endianness
http://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/
http://www.ibm.com/developerworks/aix/library/au-endianc/index.html?ca=drs-
Thanks a lot for sharing information.I admit my ignorance about number.
Have u seen any scenario where we need to reverse byte before write operation?.I could not find any purpose to reverse some byte before writing into file.


The structure is:
http://www.cplusplus.com/forum/general/105783/
Please see my response in your other thread.

Lets keep the discussion of a single issue to a single thread. It gets confusing and redundant when you have multiple threads regarding the same issue.

Topic archived. No new replies allowed.