I saw this in NSArray.h header file in the framework directory:
enum {
NSBinarySearchingFirstEqual = (1UL << 8),
NSBinarySearchingLastEqual = (1UL << 9),
NSBinarySearchingInsertionIndex = (1UL << 10),
};
typedef NSUInteger NSBinarySearchingOptions;
What’s the point of “NSBinarySearchingFirstEqual = (1UL << 8)”?And what’s the relationship between this enumeration and the “NSBinarySearchingOptions” type-definition? Thanks.
The “NSBinarySearchingFirstEqual = (1UL << 8)” etc. assign specific values to the enumeration constants. The values are chosen so that they are represented by a single bit, allowing the options to be combined with bitwise operations. The “<<” operator is a left shift; you could equivalently write this as:
Options can be combined like:
Note that
NSBinarySearchingOptionsis typedef’d to an unsigned integer, not the enum, because it can contain values that are not one of the defined enum values (when multiple values are combined).