Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,821 answers

573 users

How to convert a 64-bit number between big-endian and little-endian values in C++

3 Answers

0 votes
#include <iostream>

int main(void)
{
    // 64 bit 

    // unsigned __int64 _byteswap_uint64(unsigned __int64 value); // VS

    unsigned __int64  n = 0x8877665544332211;

    std::cout << std::hex << std::uppercase << n << "\n";

    n = _byteswap_uint64(n);

    std::cout << std::hex << n << "\n";
}



/*

8877665544332211
1122334455667788

*/

 



answered Jan 2, 2025 by avibootz
edited Jan 3, 2025 by avibootz
0 votes
#include <iostream>

uint64_t toLittleEndian(uint64_t value) {
    return ((value >> 56) & 0x00000000000000FFULL) |
           ((value >> 40) & 0x000000000000FF00ULL) |
           ((value >> 24) & 0x0000000000FF0000ULL) |
           ((value >> 8) & 0x00000000FF000000ULL) |
           ((value << 8) & 0x000000FF00000000ULL) |
           ((value << 24) & 0x0000FF0000000000ULL) |
           ((value << 40) & 0x00FF000000000000ULL) |
           ((value << 56) & 0xFF00000000000000ULL);
}

int main(void)
{
    // 64 bit 

    unsigned __int64  n = 0x8877665544332211;

    std::cout << std::hex << std::uppercase << n << "\n";

    n = _byteswap_uint64(n);

    std::cout << std::hex << n << "\n";
}



/*

8877665544332211
1122334455667788

*/

 



answered Jan 2, 2025 by avibootz
0 votes
#include <iostream>
#include <cstdint>

int main(void)
{
    // 64 bit 
 
    // uint64_t __builtin_bswap64(uint64_t x) // GCC
 
    uint64_t  n = 0x8877665544332211;
 
    std::cout << std::hex << std::uppercase << n << "\n";
 
    n = __builtin_bswap64(n);
 
    std::cout << std::hex << n << "\n";
}
 
 
 
/*
 
8877665544332211
1122334455667788
 
*/
 

 



answered Jan 2, 2025 by avibootz
...