One, the Java bitwise operator Instance--with (&), non (~), or (|), XOR (^)
1, and (&)
0 & 2 = 0
2, non (~)
~0 = 7
3, or (|)
0 & 2 = 2
4, XOR (^)
1 & 2 = 3
Second, the use of the scene. (similar to marking)
Public enum flagenums { A (1, "H"), B (2, "rich"),
C (3, "handsome"),
;
Private intbit; PrivateString desc; Flagenums (intbit, String desc) { This. bit =bit; This. desc =desc; } /*** Gets the value of the current flag decimal * *@return */ Public LongGetbit2value () {BigDecimal Posvalue=NewBigDecimal (2); Posvalue=posvalue.pow (bit); returnPosvalue.longvalue (); } /*** Determine if the flag has a * *@paramFlag *@return */ Public BooleanIsbiton (Longflag) { if(Flag & Getbit2value ()) = =Getbit2value ()) { return true; } return false; } Public intgetbit () {returnbit; } Public voidSetbit (intbit) { This. bit =bit; } PublicString GetDesc () {returndesc; } Public voidSetdesc (String desc) { This. desc =desc; }}
User personality tag, we can use Falg value to express, the personality tag combination determines the FALG value. The combination of each label with the operation is the FALG value.
The following three actions are performed on the user:
①, "high" label for users
Flag | Flagenums.a.getvalue ()
②, Erase "high" label to user
Flag & (~flagenums.a.getvalue ())
③, determine if the user has a "high" label
Flagenums.a.isbiton (flag);
Java bitwise operator Instances--with (&), non (~), or (|), XOR (^)