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,819 answers

573 users

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

3 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    // 64 bit

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

    unsigned __int64  n = 0x8877665544332211;

    printf("0x%llX\n", n);

    n = _byteswap_uint64(n);

    printf("0x%llX\n", n);
}



/*

0x8877665544332211
0x1122334455667788

*/

 



answered Jan 3, 2025 by avibootz
0 votes
#include <stdio.h>

unsigned __int64 toLittleEndian(unsigned __int64 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;

    printf("0x%llX\n", n);

    n = toLittleEndian(n);

    printf("0x%llX\n", n);
}




/*

0x8877665544332211
0x1122334455667788

*/

 



answered Jan 3, 2025 by avibootz
0 votes
#include <stdio.h>
#include <inttypes.h>

int main(void)
{
    // 64 bit 
  
    // uint64_t __builtin_bswap64(uint64_t x) // GCC
  
    uint64_t  n = 0x8877665544332211;
  
    printf("0x%" PRIX64 "\n", n);

    n = __builtin_bswap64(n);
  
    printf("0x%" PRIX64 "\n", n);
}


  
/*
  
0x8877665544332211
0x1122334455667788
  
*/
  

 



answered Jan 3, 2025 by avibootz
edited Jan 3, 2025 by avibootz
...