Skip to content

Instantly share code, notes, and snippets.

@mdutkin
Created May 17, 2018 04:41
Show Gist options
  • Save mdutkin/aef6f5c5988c82319926853a708c70e2 to your computer and use it in GitHub Desktop.
Save mdutkin/aef6f5c5988c82319926853a708c70e2 to your computer and use it in GitHub Desktop.
| is used to set a certain bit to 1
& is used to test or clear a certain bit
Set a bit (where n is the bit number, and 0 is the least significant bit):
unsigned char a |= (1 << n);
Clear a bit:
unsigned char b &= ~(1 << n);
Toggle a bit:
unsigned char c ^= (1 << n);
Test a bit:
unsigned char e = d & (1 << n);
Take the case of your list for example:
x | 2 is used to set bit 1 of x to 1
x & 1 is used to test if bit 0 of x is 1 or 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment