Reputation: 61774
In code I found some lines:
UIApplication *application = [UIApplication sharedApplication]; //1
application.applicationIconBadgeNumber = 0; //2
UIUserNotificationSettings *currentSettings = application.currentUserNotificationSettings; //3
BOOL badgeAllowed = currentSettings.types & UIUserNotificationTypeBadge; //4
I know what mean lines 1 to 3, but what means line 4?
It returns BOOL
but if I want both parts to be true there should be &&
instead of &
. Why there is only one &
? What does it mean? I would like to read some about it, but I do not know what is it.
Upvotes: 1
Views: 275
Reputation: 130082
&&
is a logical operator AND
&
is a bitwise operator AND.
That means that every bit of the resulting number is set only if both operands have that bit set.
In this specific case
currentSettings.types
is a mask of allowed notification types (a bit is set for every type).
ANDing it with UIUserNotificationTypeBadge
results in
UIUserNotificationTypeBadge
is part of the mask, only that specific bit is set in the result.UIUserNotificationTypeBadge
is not part of the mask, the result has all bits set to zero.Then the integer is considered as a boolean, which means non-zero = true, zero = false.
The last step can be written explicitly (which is better IMHO)
BOOL badgeAllowed = ((currentSettings.types & UIUserNotificationTypeBadge) != 0);
Upvotes: 2
Reputation: 16966
&
is the C bitwise AND operator. It compares each bit of the left and right operands and set the corresponding result bit to 1 if the source bits are both 1 and 0 otherwise. For your code, the bit mask UIUserNotificationTypeBadge
is used to check whether that individual bit is set in currentSettings.types
. In C all non-zero values are interpreted as true so if currentSettings.types
has the UIUserNotificationTypeBadge
bit set badgeAllowed
will be true, although not necessarily 1
.
Upvotes: 0