JMU
Bit Flags
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
An Example
Review
Thinking About the Problem
The Pattern
A Simplification
Composite Masks
A Useful Method
javaexamples/programmingpatterns/Flags.java (Fragment: anyOf)
            /**
     * Returns true if any of the bits in the parameter named needed are
     * set in the parameter named actual.
     *
     * @param needed  The flags that need to be 1
     * @param actual  The bits that are actually set
     * @return        true if any of the bits in named are set in actual
     */
    public static boolean anyOf(int needed, int actual) {
        return (needed & actual) > 0;
    }
        
Another Useful Method
javaexamples/programmingpatterns/Flags.java (Fragment: allOf)
            /**
     * Returns true if all of the bits in the parameter named needed are
     * set in the parameter named actual.
     *
     * @param needed  The flags that need to be 1
     * @param actual  The bits that are actually set
     * @return        true if all of the bits in named are set in actual
     */
    public static boolean allOf(int needed, int actual) {
        return (needed & actual) == needed;
    }
        
A Third Useful Method
javaexamples/programmingpatterns/Flags.java (Fragment: onlyOf)
            /**
     * Returns true if exactly the bits in the parameter named needed are
     * set in the parameter named actual.
     *
     * @param needed  The flags that need to be 1
     * @param actual  The bits that are actually set
     * @return        true if only the bits in named are set in actual
     */
    public static boolean onlyOf(int needed, int actual) {
        return (needed == actual);
    }
        
An Example
The Masks
javaexamples/programmingpatterns/Flags.java (Fragment: constants)
            public static final int FOOD   =  1;
    public static final int SPELL  =  2;
    public static final int POTION =  4;
    public static final int WAND   =  8;
    public static final int WATER  = 16;   
    // And so on...
        
One Player's Inventory
javaexamples/programmingpatterns/Flags.java (Fragment: initialization)
                int inventory;
        inventory = 0;
        
An Example (cont.)
Acquiring Water
javaexamples/programmingpatterns/Flags.java (Fragment: gotwater)
                inventory = inventory | WATER;
        
Acquiring the Spell
javaexamples/programmingpatterns/Flags.java (Fragment: gotspell)
                inventory |= SPELL;
        
An Example (cont.)
Checking for Water
javaexamples/programmingpatterns/Flags.java (Fragment: havewater)
                boolean haveWater = (inventory & WATER) > 0;
        
Checking for the Potion
javaexamples/programmingpatterns/Flags.java (Fragment: havepotion)
                boolean havePotion = (inventory & POTION) > 0;
        
An Example (cont.)
Using Water
javaexamples/programmingpatterns/Flags.java (Fragment: usewater)
                inventory = inventory & (WATER ^ Integer.MAX_VALUE);