C++: swapping bytes of an uint64_t This question is about swapping bytes of an uint64_t value, or in other words, about converting between endianness of an integer value:
#include &lt;algorithm&gt;
#include &lt;cstdint&gt;
#include &lt;iomanip&gt;
#include &lt;iostream&gt;

uint64_t&amp; swapBytes1(uint64_t&amp; num) {
    using std::swap;

    union {
        uint8_t bytes[8];
        uint64_t num;
    } transformer;

    transformer.num = num;

    swap(transformer.bytes[0], transformer.bytes[7]);
    swap(transformer.bytes[1], transformer.bytes[6]);
    swap(transformer.bytes[2], transformer.bytes[5]);
    swap(transformer.bytes[3], transformer.bytes[4]);

    return num = transformer.num;
}

uint64_t&amp; swapBytes2(uint64_t&amp; num) {
    using std::swap;

    union {
        struct {
            uint8_t b0;
            uint8_t b1;
            uint8_t b2;
            uint8_t b3;
            uint8_t b4;
            uint8_t b5;
            uint8_t b6;
            uint8_t b7;
        } bytes;

        uint64_t num;
    } transformer;

    transformer.num = num;

    swap(transformer.bytes.b0, transformer.bytes.b7);
    swap(transformer.bytes.b1, transformer.bytes.b6);
    swap(transformer.bytes.b2, transformer.bytes.b5);
    swap(transformer.bytes.b3, transformer.bytes.b4);

    return num = transformer.num;
}

void main() {
    uint64_t num = 0x0123456789abcdef;
    std::cout &lt;&lt; std::hex &lt;&lt; num &lt;&lt; &quot;\n&quot;;
    std::cout &lt;&lt; swapBytes1(num) &lt;&lt; &quot;\n&quot;;
    swapBytes2(num);
    std::cout &lt;&lt; num &lt;&lt; &quot;\n&quot;;
}

Critique request
As I am not a professional C++ developer, I would like to receive any comment/answer that could improve my C++ coding habits.
Technically this is UB.
Writing a value to one member of a union, then reading from another member (that was not written to) of the union is undefined behavior.
You would be better off just casting to a char* and just reversing it.
swap(std::uint64_t&amp; num)
{
    // I like the use of reinterpret_cast here
    // it makes sure that somebody reading the code notices
    // and takes special care to review what is happening.
    //
    char* first = reinterpret_cast&lt;char*&gt;(&amp;num);
    char* last  = first + sizeof(num);

    // The advantage of reverse here:
    //
    // The standard library can have pre built optimizations
    // that will use hardware specific optimizations of this that
    // can convert it to a single instruction on some hardware.
    //
    std::reverse(first, last);
}


The question becomes are you doing this to support endianness?
If so then your code is only going to work on machines that have the same endianness as the platform you are currently working on.
Normally people want to swap from platform byte order to network byte order (or the reverse). Depending on architecture this is either a swap (as you have above) or a NO-OP or for some weird hardware something completely different.
To support this we have specific family of functions:
htonl()   // host to network long  (32 byte)
htons()   // host to network short (16 byte)

ntohl()   // network to host long  (32 byte)
ntohs()   // network to host short (16 byte)

These functions will do the appropriate conversion for your hardware.