How does "&" work in this application?
I know how bitwise AND works, but I don't understand how it works (sourceDragMask and NSDragOperationGeneric), I don't understand the point. Can anyone please explain to me? Many thanks.
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSColorPboardType] )
{
if (sourceDragMask & NSDragOperationGeneric)
{
return NSDragOperationGeneric;
}
}
return NSDragOperationNone;
}
a source to share
I know how bitwise AND works, but I don't understand how (sourceDragMask and NSDragOperationGeneric) works, I don't understand.
NSDragOperationGeneric
most likely has a strength of two, which means it only has one bit. This is intentional: the bitmasks are almost all defined as powers of two (single bits) to enable bitmask operations like this one.
The bitwise-AND operation, as you know, only evaluates the bits that are set on both sides. If one side has only one bit ( NSDragOperationGeneric
), then the operation effectively checks if that bit is set on the other side.
Point of operation: Check if the bit is set NSDragOperationGeneric
.
There is one question: as you know, a successful bitwise AND test will be evaluated using bit-bit-bit rather than 1. So, for example, if you check the bit mask which is defined as 0x100 (1 and then 8 bits) then assign that result to a variable BOOL
(which is signed char
), you assign zero to the variable! This is why you sometimes see code like this:
BOOL supportsCopyOperation = ((dragOperations & NSDragOperationCopy) == NSDragOperationCopy);
or that:
BOOL supportsCopyOperation = ((dragOperations & NSDragOperationCopy) != 0);
or that:
BOOL supportsCopyOperation = !!(dragOperations & NSDragOperationCopy);
Other bitmask operations include bitwise OR ( |
) to set bits to a value ( return NSDragOperationCopy | NSDragOperationMove;
for example) and bitwise-NOT ( ~
aka two supplement) to invert the bits of a value, usually for "nothing but" tests.
a source to share
NSDragOperationGeneric
- bit mask; probably the strength of two. this means that only one bit of the number is set. Bitwise &
c sourceDragMask
will produce non-zero output if and only if the corresponding bit is set to sourceDragMask
.
Hypothetically (I don't know the actual meaning NSDragOperationGeneric
):
sourceDragMask: 00011010
NSDragOperationGeneric: 00001000
a & b: 00001000
In short, the if statement will succeed if the sourceDragMask
`NSDragOperationGeneric 'bit is set.
a source to share
sourceDragMask
is what is called a bitmask: an integer representing multiple boolean flags. It is declared as a variable of the type NSDragOperation
that is actually typedef
ed from NSUInteger
- it really is.
First, an operator &
is what is known as a "bitwise AND", which simply takes two numbers AND
from each of the bits in them to create a new value. For example, 0011 1001 & 0101 1010 == 0001 0001
- 57 & 90 = 17
. Likewise, the operator |
performs a bitwise OR. Using these operators, we can use each of these 1s and 0s as a boolean flag separately.
So if, for example, sourceDragMask
is a meaning 12
, what does that tell us? In binary, it 12
is represented as 0000 1100
or 8 | 4
, which according to the NSDraggingInfo protocol reference , NSDragOperationPrivate | NSDragOperationGeneric
So, if we do &
this with NSDragOperationGeneric
, we get 4
out, which is nonzero and therefore true. This way we can check if the bitmask contains a specific flag.
a source to share