Bitwise Operators and Bit Masks

Source: Internet
Author: User
Tags bitwise operators

1. masks

// Constants to hold bit masks for desired flagsstatic final int flagAllOff = 0;  //         000...00000000 (empty mask)static final int flagbit1 = 1;    // 2^^0    000...00000001static final int flagbit2 = 2;    // 2^^1    000...00000010static final int flagbit3 = 4;    // 2^^2    000...00000100static final int flagbit4 = 8;    // 2^^3    000...00001000static final int flagbit5 = 16;   // 2^^4    000...00010000static final int flagbit6 = 32;   // 2^^5    000...00100000static final int flagbit7 = 64;   // 2^^6    000...01000000static final int flagbit8 = 128;  // 2^^7    000...10000000//...static final int flagbit31 = (int) Math.pow(2, 30);   // 2^^30//...// Variable to hold the status of all flagsint flags = 0;

 

2. individual masks to obtain combo masks:

int maskReadAllowed = 4;    // 00001000int maskWriteAllowed = 16;  // 00010000// 00001000 | 00010000 = 00011000 represents both read and write permissionsint maskReadAndWriteAllowed = maskReadAllowed | maskWriteAllowed;// Start out with no permissionsint user1Permissions = 0;   // 00000000int user2Permissions = 0;   // 00000000// Allow user 1 to read// 00000000 | 00001000 = 00001000user1Permissions |= maskReadAllowed;// Allow user 2 to read as well as write in one shot// 00000000 | 00011000 = 00011000user2Permissions |= maskReadAndWriteAllowed;// Later, allow user 1 to write also// 00001000 | 00010000 = 00011000user1Permissions |= maskWriteAllowed;// Or, to make sure that user 1 has read permission as well, just in case// 00001000 | 00011000 = 00011000 (same as before)user1Permissions |= maskReadAndWriteAllowed;

 

3. BitMask

 

      0 0 0 0 0 0 1 0     (MASK constant)AND   1 0 0 1 0 1 1 0     (your flags variable)      ---------------      0 0 0 0 0 0 1 0     (result of operation)

 

 

4. sample

 

    // Define masks for lowest level permissions as constants which    // are powers of 2. This ensures that only one bit in an integer    // is set to 1.  Make these private because combinations will be    // used to publically access valid permission sets.
   public static final int VIEW = 4; public static final int ADD = 16; public static final int EDIT = 32; public static final int DELETE = 256;    /* Valid permission sets    - No permission to do anything    - View Only    - View and Add    - View and Edit    - View, Add and Edit    - All permissions (View, Add, Edit and Delete)    */    // Obtain valid permission sets by Bitwise ORing lower level permissions    public static final int NOTHING_ALLOWED = 0;    public static final int VIEW_ALLOWED = VIEW;    public static final int VIEW_ADD_ALLOWED = VIEW | ADD;    public static final int VIEW_EDIT_ALLOWED = VIEW | EDIT;    public static final int VIEW_ADD_EDIT_ALLOWED = VIEW | ADD | EDIT;    public static final int ALL_ALLOWED = VIEW | ADD | EDIT | DELETE;    // Or, alternately    //public static final int ALL_ALLOWED = VIEW_ADD_EDIT_ALLOWED | DELETE;

 

 

    // Check permission(s)    public static boolean isPermitted(        int myPermissions,        int permissionToCheck    ) {        return ((myPermissions & permissionToCheck) == permissionToCheck);    }

 

 

Reference:

1. Bitwise Operators and Bit Masks

2. <Apress Beginning C From Novice to Professional 4th Edition>

P119 Using Bitwise Operators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.