Skip to content

Instantly share code, notes, and snippets.

@vklachkov
Created January 25, 2020 00:42
Show Gist options
  • Save vklachkov/274c70dda437c186e12d97abf8db872f to your computer and use it in GitHub Desktop.
Save vklachkov/274c70dda437c186e12d97abf8db872f to your computer and use it in GitHub Desktop.
Macros that simplify working with individual bytes
#define GET_BYTE(val, n) (((val) >> (8*(n))) & 0xFF)
#define GET_BYTE_FROM_END(val, n) (((val) >> (8*(sizeof((val)) - 1 - (n)))) & 0xFF)
#define BSWAP16(val) \
( (((val) >> 8) & 0x00FF) | (((val) << 8) & 0xFF00) )
#define BSWAP32(val) \
( (((val) >> 24) & 0x000000FF) | (((val) >> 8) & 0x0000FF00) | \
(((val) << 8) & 0x00FF0000) | (((val) << 24) & 0xFF000000) )
#define BSWAP64(val) \
( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \
(((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \
(((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \
(((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment