Skip to content

Instantly share code, notes, and snippets.

@huoxudong125
Created June 10, 2014 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huoxudong125/992b770f5d7a978c722a to your computer and use it in GitHub Desktop.
Save huoxudong125/992b770f5d7a978c722a to your computer and use it in GitHub Desktop.
//! Byte swap unsigned short
static uint16_t swap_uint16(uint16_t val)
{
return (val << 8) | (val >> 8);
}
//! Byte swap short
static int16_t swap_int16(int16_t val)
{
return (val << 8) | ((val >> 8) & 0xFF);
}
//! Byte swap unsigned int
static uint32_t swap_uint32(uint32_t val)
{
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
return (val << 16) | (val >> 16);
}
//! Byte swap int
static int32_t swap_int32(int32_t val)
{
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
return (val << 16) | ((val >> 16) & 0xFFFF);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment