Skip to content

Instantly share code, notes, and snippets.

@cristeigabriel
Last active July 10, 2021 22:14
Show Gist options
  • Save cristeigabriel/fb6166d973ceb881d5dc3a0d5db340e2 to your computer and use it in GitHub Desktop.
Save cristeigabriel/fb6166d973ceb881d5dc3a0d5db340e2 to your computer and use it in GitHub Desktop.
uint32_t endianness swap
constexpr auto byte_swap_32bit(uint32_t bytes)
{
return (uint32_t)(bytes << 16) | (uint32_t)(bytes >> 16);
}
constexpr auto to_array_32bit(uint32_t bytes)
{
return PTL::Array<uint8_t, 4>((uint8_t)((uint32_t)(bytes & 0xff000000) >> 24),
(uint8_t)((uint32_t)(bytes & 0x00ff0000) >> 16),
(uint8_t)((uint32_t)(bytes & 0x0000ff00) >> 8),
(uint8_t)((uint32_t)(bytes & 0x000000ff)));
}
constexpr auto endianness_swap_32bit(uint32_t bytes)
{
return 0x10000 * ((uint32_t)(byte_swap_16bit((uint16_t)((bytes & 0x0000ffff))))) + (uint32_t)(byte_swap_16bit((uint16_t)((uint32_t)((bytes & 0xffff0000)) >> 16)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment