Confused by java syntax
This question already has an answer here:
这是对声明的简短陈述:
noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL;
It's a Bitwise OR operator used as assignment
noti.flags |= Notification.FLAG_AUTO_CANCEL;
is the same of
noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL
It's the assignment version of the Bitwise Or operator, ie:
noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL;
The bitwise or
does an inclusive bitwise OR operation:
10110 bitwise or
01100
-----------------
11110
From the source code:
Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.
public static final int FLAG_AUTO_CANCEL = 0x00000010;
This is hexadecimal for the number 16. If you're wondering why we use these types of flags, it's because other flags will have representations:
0x00000020
0x00000040
0x00000080
Each time, we go up by a power of 2. Converting this to binary, we get:
00010000
00100000
01000000
10000000
Hence, we can use a bitwise or
to determine which of the flags are present, since each flag is contains only one 1
and they are all in different locations.
上一篇: =运算符
下一篇: 被java语法困惑