Skip to content

Instantly share code, notes, and snippets.

@Marzogh
Last active April 19, 2018 07:15
Show Gist options
  • Save Marzogh/ef612d368718dc3941c8ec2b0d698e18 to your computer and use it in GitHub Desktop.
Save Marzogh/ef612d368718dc3941c8ec2b0d698e18 to your computer and use it in GitHub Desktop.
A list of macros that can be used to carry out bit manipulation
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Bit manipulation macros //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//Extract bytes from integers, floats & doubles
#define Lo(param) ((char *)&param)[0] //0x000y
#define Hi(param) ((char *)&param)[1] //0x00y0
#define Higher(param) ((char *)&param)[2] //0x0y00
#define Highest(param) ((char *)&param)[3] //0xy000
//Extract nibbles from integers, floats & doubles
#define Low(param) ((int *)&param)[0] //0x00yy
#define Top(param) ((int *)&param)[1] //0xyy00
// Set bit and clear bit
// x -> byte, y -> bit
#define setBit(x, y) x |= (1 << y)
#define clearBit(x, y) x &= ~(1 << y)
#define toggleBit(x, y) x ^= (1 << y)
// Query to see if bit is set or cleared.
// x -> byte, y -> bit
#define bitIsSet(x, y) x & (1 << y)
#define bitIsClear(x, y) !(x & (1 << y))
//Set nibbles
// x -> byte, y -> value to set
#define setLowerNibble(x, y) x &= 0xF0; x |= (y & 0x0F) // Clear out the lower nibble // OR in the desired mask
#define setUpperNibble(x, y) x &= 0x0F; x |= (y & 0xF0) // Clear out the lower nibble // OR in the desired mask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment