Created
February 24, 2015 00:23
-
-
Save brandonto/f36df9b948e5e1d3bb73 to your computer and use it in GitHub Desktop.
Setting/Getting a specific bit in an unsigned char
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// 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