Skip to content

Instantly share code, notes, and snippets.

@brandonto
Created February 24, 2015 00:23
Show Gist options
  • Save brandonto/f36df9b948e5e1d3bb73 to your computer and use it in GitHub Desktop.
Save brandonto/f36df9b948e5e1d3bb73 to your computer and use it in GitHub Desktop.
Setting/Getting a specific bit in an unsigned char
/// x: 8-bit value. k: bit position to set, range is 0-7. b: set bit to this, either 1 or 0
unsigned char SetBit(unsigned char x, unsigned char k, unsigned char b) {
return (b ? (x | (0x01 << k)) : (x & ~(0x01 << k)) );
// Set bit to 1 Set bit to 0
}
unsigned char GetBit(unsigned char x, unsigned char k) {
return ((x & (0x01 << k)) != 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment