A Simple Application of Bit Operations-Rights Management

Personal Blog

http://www.milovetingting.cn

A Simple Application of Bit Operations-Rights Management

Bit operations have many clever scenarios in actual development.For example:

  1. An array of positive integers with one number appearing only once and the other numbers appearing twice. Seek for a number appearing only once.

  2. Swap the values of two numbers without a temporary variable.

  3. Window s Settings Flag in Android

    public void setFlags(int flags, int mask) {
        final WindowManager.LayoutParams attrs = getAttributes();
        attrs.flags = (attrs.flags&~mask) | (flags&mask);
        mForcedWindowFlags |= mask;
        dispatchWindowAttributesChanged(attrs);
    }
  1. Products have features that can be stored using bitwise operations.

Here's a simple simulation of rights management using bitwise operations

Permission Management Class

public class PermissionManager {

    public static final int PERMISSION_NONE = 1;

    public static final int PERMISSION_A = 1 << 1;

    public static final int PERMISSION_B = 1 << 2;

    public static final int PERMISSION_C = 1 << 3;

    public static final int PERMISSION_D = 1 << 4;

    private int mPermission = PERMISSION_NONE;

    private static class PermissionManagerHolder {
        static PermissionManager sPermissionManager = new PermissionManager();
    }

    private PermissionManager() {

    }

    public static PermissionManager getInstance() {
        return PermissionManagerHolder.sPermissionManager;
    }

    /**
     * add permission
     * @param permission
     */
    public void addPermission(int permission) {
        if (hasPermission(permission)) {
            return;
        }
        mPermission |= permission;
    }

    /**
     * Remove permissions
     * @param permission
     */
    public void removePermission(int permission) {
        if (!hasPermission(permission)) {
            return;
        }
        mPermission &= ~permission;
    }

    /**
     * Determine whether you have permission
     * @param permission
     * @return
     */
    public boolean hasPermission(int permission) {
        return (mPermission & permission) != 0;
    }

}

Use

public static void main(String[] args) {
        System.out.println("No permissions added----------");
        PermissionManager permissionManager = PermissionManager.getInstance();
        checkPermission(permissionManager);
        System.out.println("add permission A----------");
        System.out.println("add permission B----------");
        System.out.println("add permission C----------");
        permissionManager.addPermission(PermissionManager.PERMISSION_A);
        permissionManager.addPermission(PermissionManager.PERMISSION_B);
        permissionManager.addPermission(PermissionManager.PERMISSION_C);
        checkPermission(permissionManager);
        System.out.println("Delete permissions C----------");
        permissionManager.removePermission(PermissionManager.PERMISSION_C);
        checkPermission(permissionManager);
    }

    public static void checkPermission(PermissionManager permissionManager) {
        System.out.println("PERMISSION_A exist:" + permissionManager.hasPermission(PermissionManager.PERMISSION_A));
        System.out.println("PERMISSION_B exist:" + permissionManager.hasPermission(PermissionManager.PERMISSION_B));
        System.out.println("PERMISSION_C exist:" + permissionManager.hasPermission(PermissionManager.PERMISSION_C));
        System.out.println("PERMISSION_D exist:" + permissionManager.hasPermission(PermissionManager.PERMISSION_D));
    }

Output results:

No permissions added----------
PERMISSION_A exist:false
PERMISSION_B exist:false
PERMISSION_C exist:false
PERMISSION_D exist:false
//Add Permission A------------------------------------------------------------------------------------------------------------
//Add Permission B--------------------------------------------------------------------.
//Add Permission C------------------------------------------------------------------------------------------------------------
PERMISSION_A exist:true
PERMISSION_B exist:true
PERMISSION_C exist:true
PERMISSION_D exist:false
//Delete permission C--------------------------------------------------------------------------------------------------------
PERMISSION_A exist:true
PERMISSION_B exist:true
PERMISSION_C exist:false
PERMISSION_D exist:false

Bit operations are not limited to privilege management and can be used in scenarios with multiple states.

Tags: Android

Posted on Thu, 16 Jan 2020 11:56:13 -0500 by inversesoft123