View Single Post
Codemonkey's Avatar Codemonkey Codemonkey is offline Super Moderator 2006-11-07 #26 Old  
023 - List all flags in a combined variable
Last edited by Codemonkey : 2006-11-12 at 01:45.
&nbsp
Sometimes it is useful to list all the single flags in a combined flag number. For example for debugging, logging or some other operation. You can do this by AND'ing all single flags by hand like this:

ActionScript Code:
  1. var FLAGONE = 1;
  2. var FLAGTWO = 2;
  3. var FLAGTHREE = 4;
  4.  
  5. var combinedFlag = FLAGONE | FLAGTHREE;
  6.  
  7. trace("FLAGONE: " + (combinedFlag & FLAGONE)); // traces "1"
  8. trace("FLAGTWO: " + (combinedFlag & FLAGTWO)); // traces "0"
  9. trace("FLAGTHREE: " + (combinedFlag & FLAGTHREE)); // traces "4"
  10.  
Everything that doesn't result in 0 means the flag is in it. Remember for example 16 only means the flag with the bit 5 (from the right) set. This can become cumbersome though when you have a lot of flags or when you add or remove flags. You want a dynamic way that lists all the flags in a combined flag. Here is one way:

ActionScript Code:
  1. // create the flags and put them in an array we'll
  2. // use to run through for tracing separate flags
  3. var flags:Array = new Array();
  4. var FLAGONE = 1; flags.push(FLAGONE);
  5. var FLAGTWO = 2; flags.push(FLAGTWO);
  6. var FLAGTHREE = 4; flags.push(FLAGTHREE);
  7.  
  8. // return a list of flags that are in the combined flag
  9. function listFlags(combinedFlag:Number): Array {
  10.         var result:Array = new Array();
  11.         for (var flag in flags) {
  12.                 // check if flag is in the combined flag
  13.                 if (combinedFlag & flags[flag]) {
  14.                         result.push(flags[flag]);
  15.                 }
  16.         }
  17.         return result;
  18. }
  19.  
  20. var combinedFlag = FLAGONE | FLAGTHREE;
  21. trace(listFlags(combinedFlag)); // traces "4, 1"
  22.  
There's one problem though. This method returns only the values of those individual flags, not their names. If you want their names as well (or some other description), create the constants another way:

ActionScript Code:
  1. // create the flags and put them in an array including descriptions
  2. // we'll use to run through for tracing separate flags
  3. var flags:Array = new Array();
  4. var FLAGONE = 1; flags[FLAGONE] = [FLAGONE, "Flag 1 for some purpose"];
  5. var FLAGTWO = 2; flags[FLAGTWO] = [FLAGTWO, "Flag 2"];
  6. var FLAGTHREE = 4; flags[FLAGTHREE] = [FLAGTHREE, "Flag 3 yay"];
  7.  
  8. // return a list of flags that are in the combined flag
  9. function listFlags(combinedFlag:Number): Array {
  10.         var result:Array = new Array();
  11.         for (var flag in flags) {
  12.                 // check if flag is in the combined flag
  13.                 if (combinedFlag & flags[flag][0]) {
  14.                     // add the flag's description
  15.                     result.push(flags[flag][1]);
  16.                 }
  17.         }
  18.         return result;
  19. }
  20.  
  21. var combinedFlag = FLAGONE | FLAGTHREE;
  22. trace(listFlags(combinedFlag));
  23. // traces:
  24. // "Flag 1 for some purpose"
  25. // "Flag 3 yay"
  26.