Transferred from: http://xxgblog.com/2013/09/15/java-bitmask/
In Java, there are many bit operators, such as (&), non (~), or (|), XOR (^), Shift (<< and >>), and so on. These operators are seldom used in everyday coding.
In the following example, the in-place mask (bitmask) is used, which contains a large number of bit operations. Not only in Java, but also in other written languages.
For example, in a system, the user generally has the query (Select), New (Insert), modify (Update), remove (delete) Four kinds of permissions, four kinds of permissions have a variety of combinations, that is, there are 16 different permission states (2 of 4).
Permission
In general, you would think of using four Boolean variables to save:
Public classPermission {//whether to allow queries Private BooleanAllowselect; //whether to allow new Private BooleanAllowinsert; //whether to allow deletion Private BooleanAllowDelete; //whether to allow updates Private Booleanallowupdate; //omit getter and setter}
The above uses four Boolean variables to hold each permission state.
Newpermission
Here is another way, using a bit mask, you can use a binary number, each bit to represent a permission, 0 means no permissions, 1 means that there is permission.
Public classnewpermission {//whether to allow query, binary 1th bit, 0 means no, 1 means yes Public Static Final intAllow_select = 1 << 0;//0001//allow new, binary 2nd bit, 0 No, 1 means yes Public Static Final intAllow_insert = 1 << 1;//0010//whether to allow modification, binary 3rd bit, 0 means no, 1 means yes Public Static Final intAllow_update = 1 << 2;//0100//Whether delete is allowed, binary 4th bit, 0 means no, 1 means yes Public Static Final intAllow_delete = 1 << 3;// +//Store the current permission state Private intFlag; /*** Reset Permissions*/ Public voidSetPermission (intpermission) {Flag=permission; } /*** Add one or more permissions*/ Public voidEnableintpermission) {Flag|=permission; } /*** Delete one or more permissions*/ Public voidDisableintpermission) {Flag&= ~permission; } /*** Do you have certain privileges ?*/ Public BooleanIsallow (intpermission) { return(flag & permission) = =permission; } /*** Some permissions are disabled*/ Public BooleanIsnotallow (intpermission) { return(flag & permission) = = 0; } /*** Do you only have certain permissions*/ Public BooleanIsonlyallow (intpermission) { returnFlag = =permission; }}
In the code above, the permission entries for each BITS code are represented by four constants.
For example:
Allow_select = 1 << 0 turns into binary is 0001, binary first indicates SELECT permission.
Allow_insert = 1 << 1 turns into binary is 0010, binary second indicates INSERT permission.
private int flag stores the enabled and deactivated states of various permissions, which is equivalent to replacing the four Boolean variables in permission.
The state of four permissions is represented by the four bits of flag, and the 0 and 1 of each bit represent the activation and deactivation of a permission, and the following lists the permissions that are represented by some states:
Flag |
Delete |
Modify |
New |
Inquire |
|
1 (0001) |
0 |
0 |
0 |
1 |
Allow only queries (i.e. equals Allow_select) |
2 (0010) |
0 |
0 |
1 |
0 |
Allow only new (that is, equal to Allow_insert) |
4 (0100) |
0 |
1 |
0 |
0 |
Allow only modification (that is, equal to allow_update) |
8 (1000) |
1 |
0 |
0 |
0 |
Allow only delete (i.e. equals Allow_delete) |
3 (0011) |
0 |
0 |
1 |
1 |
Allow only queries and new |
0 |
0 |
0 |
0 |
0 |
None of the four permissions are allowed |
15 (1111) |
1 |
1 |
1 |
1 |
All four permissions allow |
With a bitmask, you only need to use an integer greater than or equal to 0 and less than 16 to represent the state of all 16 permissions.
In addition, there are many ways to set permissions and determine permissions, which need to be used in place, for example:
Public void enable (int permission) { // equals flag = Flag | permission;}
Call this method to add one or more permissions on the basis of an existing permission.
Add an UPDATE permission:
Permission.enable (newpermission.allow_update);
Assume that the existing permission is only select, which is flag 0001. Execute the above code, flag = 0001 | 0100, which is 0101, has the Select and update two permissions.
Add Insert, Update, delete three permissions:
permission.enable (Newpermission.allow_insert | Newpermission.allow_update | Newpermission.allow_delete);
Newpermission.allow_insert | Newpermission.allow_update | The result of the newpermission.allow_delete operation is 1110. Assume that the existing permission is only select, which is flag 0001. Flag = 0001 | 1110, which is 1111, has these four permissions, equivalent to adding three permissions.
The above setting, if you use the original permission class, requires the following three lines of code:
Permission.setallowinsert (true);p ermission.setallowupdate (true); Permission.setallowdelete (true);
The comparison setting allows only select and insert Permissions
Permission
Permission.setallowselect (true);p Ermission.setallowinsert (true); Permission.setallowupdate (false);p ermission.setallowdelete (false);
Newpermission
Permission.setpermission (Newpermission.allow_select | Newpermission.allow_insert);
Determine if Select and INSERT, UPDATE permissions are allowed
Permission
if (Permission.isallowselect () && Permission.isallowinsert () && permission.isallowupdate ())
Newpermission
if (permission. Isallow (newpermission.allow_select | Newpermission.allow_insert | Newpermission.allow_update))
Decide whether to allow select and insert permissions only
Permission
if (Permission.isallowselect () && permission.isallowinsert () &&! Permission.isallowupdate () &&!permission.isallowdelete ())
Newpermission
if (Permission. Isonlyallow (Newpermission.allow_select | Newpermission.allow_insert))
In contrast, we can feel the advantage of the mypermission bitmask in relation to the permission, which can save a lot of code, the bit operation is the underlying operation, the efficiency is very high, and the understanding is very simple.
Some source code with a mask in place java.lang.reflect.Modifier
In Java reflection, Java.lang.reflect.Modifier is used to determine which modifiers are included in a class, member variable, method, and so on. In the source code of modifier, you can see:
Public Static Final intpublic = 0x00000001; Public Static Final intPRIVATE = 0x00000002; Public Static Final intPROTECTED = 0x00000004; Public Static Final intSTATIC = 0x00000008; Public Static Final intFINAL = 0x00000010; Public Static Final intSYNCHRONIZED = 0x00000020;// ...... Public Static BooleanIsprotected (intMoD) { return(MoD & PROTECTED)! = 0;} Public Static BooleanIsStatic (intMoD) { return(MoD & STATIC)! = 0;}
Android.text.util.Linkify
In Android development, Linkify can set the text address, phone, email, etc. to support the click Link:
Linkify.addlinks (TextView, Linkify.web_urls | linkify.email_addresses);
Android.text.util.Linkify Part Source:
Public Static Final intWeb_urls = 0x01; Public Static Final intEmail_addresses = 0x02; Public Static Final intPhone_numbers = 0x04; Public Static Final intMap_addresses = 0x08; Public Static Final intall = Web_urls | email_addresses | Phone_numbers |map_addresses; Public Static Final BooleanAddlinks (spannable text,intmask) { if(Mask = = 0) { return false; } urlspan[] old= Text.getspans (0, Text.length (), Urlspan.class); for(inti = old.length-1; I >= 0; i--) {Text.removespan (old[i]); } ArrayList<LinkSpec> links =NewArraylist<linkspec>(); if(Mask & web_urls)! = 0) {gatherlinks (links, text, Patterns.web_url,NewString[] {"HTTP//", "https://", "rtsp://"}, Surlmatchfilter,NULL); } if(Mask & email_addresses)! = 0) {gatherlinks (links, text, patterns.email_address,NewString[] {"mailto:" }, NULL,NULL); } if(Mask & phone_numbers)! = 0) {gatherlinks (links, text, Patterns.phone,NewString[] {"Tel:"}, Sphonenumbermatchfilter, Sphonenumbertransformfilter); } if(Mask & map_addresses)! = 0) {gathermaplinks (links, text); } pruneoverlaps (links); if(links.size () = = 0) { return false; } for(Linkspec link:links) {applylink (Link.url, Link.start, link.end, text); } return true;}
The use of Java bit operations in programming: Bitmask (bitmask)