- Forward


Bit Flags
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back Forward
  • The General Idea:
    • The flow of a program is often controlled using one or more flags, binary values (i.e., yes/no, on/off, true/false) that indicate the current or desired state of the system
  • The Issue:
    • There can be a large number of such flags that need to be managed and operated on
An Example
Back Forward
  • The Setting:
    • An adventure game in which there are a variety of different items that players can collect and put in their inventory
  • What's Clear:
    • The program will include a variety of if statements that will have boolean expressions that involve the variables that represent the items
  • What's Less Clear:
    • How to manage and operate on these variables
Review
Back Forward
  • One Solution:
    • Have a boolean variable for each item that is assigned true when the player has the item and false otherwise
  • The Shortcoming:
    • There may be a lot of them, and they will all need to be passed to methods that need them
Thinking About the Problem
Back Forward
  • Some Observations:
    • One bit can represent such a flag
    • An int is represented using multiple bits
  • A 4-Bit Integer:
    • images/Base2.svg
The Pattern
Back Forward
  • The Idea:
    • Use a non-neagtive integer to hold multiple flags and operate on it using the bit-wise &, |, and ^ operators
  • The Process:
    • Create masks that represent each of the binary states of interest, each of which is a unique power of 2
    • Declare a variable that will hold the bit flags
    • As needed, set particular bits (i.e., make the bits 1) using the | operator, clear particular bits (i.e., make the bits 0) using the & operator, and/or toggle particular bits (i.e., switch the bits to their other value) using the ˆ operator
    • As needed, check the value of particular bit using the | operator and a relational operator
A Simplification
Back Forward
  • Assume:
    • The variable being used can be represented using 8 bits, the left-most being used for the sign (0 denoting positive)
  • The Individual Masks:
    • There are seven different unique positive masks, 00000001,00000010,00000100, 00001000,00010000,00100000, and 01000000
Composite Masks
Back Forward
  • Creation:
    • Use the | operator
  • An Example:
    •           00000001
              | 01000000
                ________
                01000001
      	
A Useful Method
Back Forward
javaexamples/programmingpatterns/Flags.java (Fragment: anyOf)
 
Another Useful Method
Back Forward
javaexamples/programmingpatterns/Flags.java (Fragment: allOf)
 
A Third Useful Method
Back Forward
javaexamples/programmingpatterns/Flags.java (Fragment: onlyOf)
 
An Example
Back Forward
The Masks
javaexamples/programmingpatterns/Flags.java (Fragment: constants)
 
One Player's Inventory
javaexamples/programmingpatterns/Flags.java (Fragment: initialization)
 
An Example (cont.)
Back Forward
Acquiring Water
javaexamples/programmingpatterns/Flags.java (Fragment: gotwater)
 
Acquiring the Spell
javaexamples/programmingpatterns/Flags.java (Fragment: gotspell)
 
An Example (cont.)
Back Forward
Checking for Water
javaexamples/programmingpatterns/Flags.java (Fragment: havewater)
 
Checking for the Potion
javaexamples/programmingpatterns/Flags.java (Fragment: havepotion)
 
An Example (cont.)
Back Forward
Using Water
javaexamples/programmingpatterns/Flags.java (Fragment: usewater)
 
There's Always More to Learn
Back -