在java.regex.Pattern中有以下声明:
public final class Pattern implements java.io.Serializable {
...
private static final int UNIX_LINES = 0x01;
private static final int CASE_INSENSITIVE = 0x02;
private static final int COMMENTS = 0x04;
private static final int MULTILINE = 0x08;
private static final int LITERAL = 0x10;
private static final int DOTALL = 0x20;
private static final int UNICODE_CASE = 0x40;
private static final int CANON_EQ = 0x80;
private static final int UNICODE_CHARACTER_CLASS = 0x100;
private Pattern(String p, int f) {
if ((f & ~ALL_FLAGS) != 0) {
throw new IllegalArgumentException("Unknown flag 0x"
+ Integer.toHexString(f));
}
...
}
我正在尝试了解检查是否存在未知标志的逻辑。
为十六进制字符串
ALL_FLAGS => 00000000000000000000000111111111
~ALL_FLAGS => 11111111111111111111111000000000
现在,如果我使用标志0x03(不应使用),则
0x03 & ~ALL_FLAGS => 00000000000000000000000000000000
尽管未定义,该标志也将被允许。仅允许使用> 511的标志。
该逻辑背后的原因是什么?