Skip to content

Instantly share code, notes, and snippets.

@mikeash
Created September 20, 2010 22:02
Show Gist options
  • Save mikeash/588731 to your computer and use it in GitHub Desktop.
Save mikeash/588731 to your computer and use it in GitHub Desktop.
#define BIT_SET(array, bit) (array[bit / 8] |= (1 << (bit % 8)))
#define BIT_CLEAR(array, bit) (array[bit / 8] &= ~(1 << (bit % 8)))
#define BIT_GET(array, bit) ((array[bit / 8] | (1 << (bit % 8))) != 0)
uint8_t array[5];
bzero(array, sizeof(array);
BIT_SET(array, 18);
BIT_GET(array, 18);
@Kentzo
Copy link

Kentzo commented Sep 21, 2010

Why don't just use std::bitset? :)

@mikeash
Copy link
Author

mikeash commented Sep 21, 2010

  1. I don't like C++.
  2. More important: this was a quickie example of how zero-based array indexing can be easier to work with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment