1.masks
// Constants to hold bit masks for desired flags static final int flagAllOff = 0; // 000...00000000 (empty mask) static final int flagbit1 = 1; // 2^^0 000...00000001 static final int flagbit2 = 2; // 2^^1 000...00000010 static final int flagbit3 = 4; // 2^^2 000...00000100 static final int flagbit4 = 8; // 2^^3 000...00001000 static final int flagbit5 = 16; // 2^^4 000...00010000 static final int flagbit6 = 32; // 2^^5 000...00100000 static final int flagbit7 = 64; // 2^^6 000...01000000 static 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 flags int flags = 0;
2.individual masks to obtain combo masks:
int maskReadAllowed = 4; // 00001000 int maskWriteAllowed = 16; // 00010000 // 00001000 | 00010000 = 00011000 represents both read and write permissions int maskReadAndWriteAllowed = maskReadAllowed | maskWriteAllowed; // Start out with no permissions int user1Permissions = 0; // 00000000 int user2Permissions = 0; // 00000000 // Allow user 1 to read // 00000000 | 00001000 = 00001000 user1Permissions |= maskReadAllowed; // Allow user 2 to read as well as write in one shot // 00000000 | 00011000 = 00011000 user2Permissions |= maskReadAndWriteAllowed; // Later, allow user 1 to write also // 00001000 | 00010000 = 00011000 user1Permissions |= 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.Bit Mask
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