18
typedef unsigned char Byte;

...

void ReverseBytes( void *start, int size )
{
    Byte *buffer = (Byte *)(start);

    for( int i = 0; i < size / 2; i++ ) {
        std::swap( buffer[i], buffer[size - i - 1] );
    }
}

What this method does right now is it reverses bytes in memory. What I would like to know is, is there a better way to get the same effect? The whole "size / 2" part seems like a bad thing, but I'm not sure.

EDIT: I just realized how bad the title I put for this question was, so I [hopefully] fixed it.

1
  • Your example seems flawed, how can you swap two chars without a location? I suspect you need pass in the address. Commented Feb 25, 2009 at 16:45

5 Answers 5

34

The standard library has a std::reverse function:

#include <algorithm>
void ReverseBytes( void *start, int size )
{
    char *istart = start, *iend = istart + size;
    std::reverse(istart, iend);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I should have looked it up before writing it myself.
@JulesGagnon-Marchand: The "as-if" rule means that the description in the standard only specifies the observable effects; reverse does not actually have to be implemented that way. It may be implemented in a more optimized way, perhaps including hand-written optimizations that the compiler won't do for OP's manual version. So I would say it's certainly cleaner, no worse in terms of performance, and at least potentially better.
28

A performant solution without using the STL:

void reverseBytes(void *start, int size) {
    unsigned char *lo = start;
    unsigned char *hi = start + size - 1;
    unsigned char swap;
    while (lo < hi) {
        swap = *lo;
        *lo++ = *hi;
        *hi-- = swap;
    }
}

Though the question is 3 ½ years old, chances are that someone else will be searching for the same thing. That's why I still post this.

2 Comments

The chance has realized :)
To complete the code, just add the casting: (unsigned char*)start + size - 1;
2

If you need to reverse there is a chance that you can improve your algorithms and just use reverse iterators.

2 Comments

It's for reading data from files that uses different endiannesses.
@kitchen, this seems more like reversing the bytes of an integer, not the bytes of a whole array...
1

If you're reversing binary data from a file with different endianness you should probably use the ntoh* and hton* functions, which convert specified data sizes from network to host order and vice versa. ntohl for instance converts a 32 bit unsigned long from big endian (network order) to host order (little endian on x86 machines).

2 Comments

This question concerns swapping an array, so the solutions provided above specifically answer than need.
Or see endian.h if you're using glib.
0

I would review the stl::swap and make sure it's optimized; after that I'd say you're pretty optimal for space. I'm reasonably sure that's time-optimal as well.

1 Comment

Nowhere near time-optimal. The size/2 calculation might be optimized out of being run every loop, but the size-i-1 calculation wouldn't be, nor would the cost of the array indexing. That said, a perfectly optimized loop wouldn't be that much faster than what he's got.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.