Skip to content

Instantly share code, notes, and snippets.

@NicholasPeterson
Last active December 14, 2015 17:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicholasPeterson/5122235 to your computer and use it in GitHub Desktop.
Save NicholasPeterson/5122235 to your computer and use it in GitHub Desktop.
Simple mask iteration. Does not care about subsets.
// Use NS_OPTIONS for some compiler sugar.
//
// Use a type that fits your struct but consider using
// types directly to avoid 32/64bit width inconsistencies.
typedef NS_OPTIONS(UInt16, MaskType) {
MaskTypeOptionNone = 0,
MaskTypeOptionOne = 1 << 0,
MaskTypeOptionTwo = 1 << 1,
};
- (void)iterateOverBitmask:(MaskType)mask {
for (NSInteger i = 0; i <= (sizeof(MaskType) * 8); i++) {
// You may use currentBit as if it was mask with only the one bit set
MaskType currentBit = (1 << i);
if ((mask & currentBit)) {
// Hit!
} else {
// Miss :-(
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment