9
heyhey, just have a question about bitmasks. I think I know now what they are and where they can be used. I want to store specific permissions like BUILD, BREAK and INTERACT and maybe more for specific groups. The code below should do this but Im not very sure whether this is the corrent "style".
The idea is to use the first 3 bits here to store the permissions of the first group, then use the next three bits for the second group and so on. So my question is now whether this is a good way or not or what would be better?
public class Test {
private int permissions = 0;
/**
* The amount of permissions, currently: {@link #BREAK}, {@link #BUILD}, {@link #INTERACT}
*/
private static final int PERMISSIONS = 3;
/**
* The different permissions
*/
public static final int BUILD = 1, BREAK = 2, INTERACT = 4;
/**
* The different groups
*/
public static final int ALLIANCE = 0, OUTSIDERS = 1;
public void setPermissions(int permissions, int group)
{
this.permissions = permissions << group * PERMISSIONS;
}
public void addPermissions(int permission, int group)
{
setPermissions(this.permissions | permission, group);
}
public boolean hasPermission(int permission, int group)
{
return (permissions & permission << group * PERMISSIONS) == permission;
}
}
EDIT: I want to use as little memory as possible because I'll need to store much data.
EDIT: I also need to store it in a sql database, but it should not make probs.