Last active
December 14, 2015 17:28
-
-
Save NicholasPeterson/5122235 to your computer and use it in GitHub Desktop.
Simple mask iteration. Does not care about subsets.
This file contains 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
// 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