5 Replies Latest reply on Jun 21, 2007 7:52 AM by Greg Dove

    Binary Flags

    Rothrock Level 5
      I'm making some games where the user needs to select/check several different boxes and leave several others unchecked. I have put it in a binary "flag" what the correct answer is. That is:


      Reading from the rights side of the number, that would be box 1 unchecked, box 2 checked, box 3 unchecked, box 4 checked, and box 5 checked. (There may be more boxes, but they are all unchecked and leading zeros don't show.)

      I've worked out th different bitwise operations to tell me if they are correct, incorrect, or a mixture. But I'm trying to figure out how to give feed back at the end. Basically I want to highlight all the ones that should have been checked and hide all the ones that should not have been checked, i.e. if there is a zero in the flag hide the corresponding box and if there is a one put a highlight color on the box.

      How does one quickly read through a binary number and see which are ones and which are zeros?
        • 1. Re: Binary Flags
          kglad Adobe Community Professional & MVP
          well, unfortunately flash doesn't see that as a binary number. so, you have to convert it to a decimal and then you can use flash'es bitwise arithmetic:

          • 2. Re: Binary Flags
            Rothrock Level 5
            Thanks. I was stumbling toward something like that myself – not nearly as simple, but the same general idea. Glad to know that I'm not just totally overlooking something.
            • 3. Re: Binary Flags
              Greg Dove Level 4
              Where I do things like this I tend to use a string representation of the binary flags. This approach is flexible - you can easily split it to an array - or retrieve it from an array of individual flags via join("") - I tend to do this- , convert it to a decimal number (e.g. if you wanted to use the flash binary operators), or just check the individual flags based on the index in the string. I used this approach recently to measure uninterrupted progression from start of a loading queue (i..e. queue completion without gaps) ... very simple after each character flag is set, its just indexOf("0") if you set a flag of "1" for a loaded clip.

              var binString = "0110101"

              function isChecked(bin, idx) {
              return Boolean(bin.substr(idx,1))

              trace('decimal for currentbinary representation is :'+parseInt(binString,2))
              trace ('is third checkbox checked?:'+isChecked(binString,2))
              • 4. Re: Binary Flags
                Rothrock Level 5
                Nice. I like that too. Except it always returns true! I think that Flash 9 has changed the whole zero is false and one is true thing. So I need to change it around a bit. Also I'm using it as a number so I want to read from right to left, but still talk about the 1st position, 2nd position etc. So I think I would want to use:

                var binString:String = "0110101"

                function isChecked(bin:String, idx:Number):Boolean {
                return (bin.substr(-(idx+1),1)) == 1 ? true : false

                • 5. Binary Flags
                  Greg Dove Level 4
                  Oh - sorry about that - no its not a change with fp9 - I typed it quickly and didn't test it for both cases. It will work if its converted from string to Number , but Boolean ("0") returns true... charAt is probably even easier too (I keep forgetting the basics):

                  function isChecked(bin:String, idx:Number) {
                  idx = bin.length-idx;
                  return (bin.charAt(idx)=="1")