In reference, and in support of @vincent osinga's answer.. Here is that code, wrapped in a C-function.. that returns the binary "string" from an NSUInteger.. perfect for logging bitwise typedef's, etc.
- (NSString*) bitString:(NSUInteger) mask{
NSString *str = @"";
for (NSUInteger i = 0; i < 8 ; i++) {
// Prepend "0" or "1", depending on the bit
str = [NSString stringWithFormat:@"%@%@",
mask & 1 ? @"1" : @"0", str];
mask >>= 1;
}
return str;
}
I don't think NSInteger numberCopy = theNumber; is necessary as you're not using a pointer, but simply the primitive value as an argument, // so you won't change your original value. This will enable use as / yield results like...
NSEventType anEvent = NSLeftMouseUp|NSLeftMouseDown;
NSLog(@"%@, %u\n%@, %u\n%@, %u\n%@, %u",
bitString( NSScrollWheel), NSScrollWheel,
bitString( NSLeftMouseUp|NSLeftMouseDown),
NSLeftMouseUp|NSLeftMouseDown,
bitString( anEvent ), anEvent,
bitString( NSAnyEventMask ), NSAnyEventMask);
NSLOG ➞
00010110, 22 /* NSScrollWheel */
00000011, 3 /* NSLeftMouseUp | NSLeftMouseDown */
00000011, 3 /* same results with typedef'ed variable */
11111111, 4294967295 /* NSAnyEventMask */