title: Bitmask in PHP for settings?
body: <p>Bits and bitmask are something I have been struggling to understand for a while, but I would like to learn how to use them for settings and things like that in PHP.</p>
  
  <p>I have finally found a class that claims to do exactly that, and as I can tell, it seems to work, but I am not sure if it is the best way of doing this. I will post the class file with example code below to show it in working order.</p>
  
  <p>Please if you have experience, tell me if it can be improved, for performance or anything else. I really want to learn this, and I have been reading up on it, but it is a difficult one for me to grasp so far.</p>
  
  <p>The class...</p>
  
  <pre><code>&lt;?php
      class bitmask
      {
          /**
           * This array is used to represent the users permission in usable format.
           *
           * You can change remove or add valuesto suit your needs.
           * Just ensure that each element defaults to false. Once you have started storing
           * users permsisions a change to the order of this array will cause the
           * permissions to be incorectly interpreted.
           *
           * @type Associtive array
           */
          public $permissions = array(
                                      "read" =&gt; false,
                                      "write" =&gt; false,
                                      "delete" =&gt; false,
                                      "change_permissions" =&gt; false,
                                      "admin" =&gt; false
                                      );
  
          /**
           * This function will use an integer bitmask (as created by toBitmask())
           * to populate the class vaiable
           * $this-&gt;permissions with the users permissions as boolean values.
           * @param int $bitmask an integer representation of the users permisions.
           * This integer is created by toBitmask();
           *
           * @return an associatve array with the users permissions.
           */
          public function getPermissions($bitMask = 0)
          {
              $i = 0;
              foreach ($this-&gt;permissions as $key =&gt; $value)
              {
                  $this-&gt;permissions[$key] = (($bitMask &amp; pow(2, $i)) != 0) ? true : false;
  
                  // Uncomment the next line if you would like to see what is happening.
                  //echo $key . " i= ".strval($i)." power=" . strval(pow(2,$i)). "bitwise &amp; = " . strval($bitMask &amp; pow(2,$i))."&lt;br&gt;";
                  $i++;
              }
              return $this-&gt;permissions;
          }
  
          /**
           * This function will create and return and integer bitmask based on the permission values set in
           * the class variable $permissions. To use you would want to set the fields in $permissions to true for the permissions you want to grant.
           * Then call toBitmask() and store the integer value.  Later you can pass that integer into getPermissions() to convert it back to an assoicative
           * array.
           *
           * @return int an integer bitmask represeting the users permission set.
           */
          function toBitmask()
          {
              $bitmask = 0;
              $i = 0;
              foreach ($this-&gt;permissions as $key =&gt; $value)
              {
  
                  if ($value)
                  {
                      $bitmask += pow(2, $i);
                  }
                  $i++;
              }
              return $bitmask;
          }
      }
  ?&gt;
  </code></pre>
  
  <p>How do I set/save the permissions as a bitmask value?</p>
  
  <pre><code>&lt;?php
      /**
       * Example usage
       * initiate new bitmask object
       */
      $perms = new bitmask();
  
      /**
       * How to set permissions for a user
       */
      $perms-&gt;permissions["read"] = true;
      $perms-&gt;permissions["write"] = true;
      $perms-&gt;permissions["delete"] = true;
      $perms-&gt;permissions["change_permissions"] = true;
      $perms-&gt;permissions["admin"] = false;
  
      // Converts to bitmask value to store in database or wherever
      $bitmask = $perms-&gt;toBitmask();  //in this example it is 15
      $sql = "insert into user_permissions (userid,permission) values(1,$bitmask)";
      echo $sql; //you would then execute code to insert your sql.
  ?&gt;
  </code></pre>
  
  <p>Example of taking the bitmask value and returning a true/false for each array item based on the bit value....</p>
  
  <pre><code>&lt;?php
      /**
       * Example usage to get the bitmask value from database or session/cache.... then put it to use.
       * $permarr returns an array with true/false for each array value based on the bit value
       */
      $permarr = $perms-&gt;getPermissions($bitmask);
  
      if ($permarr["read"])
      {
          echo 'user can read: &lt;font color="green"&gt;TRUE&lt;/font&gt;';
      }
      else {
          echo 'user can read: &lt;font color="red"&gt;FALSE&lt;/font&gt;';
      }
  
      //user can WRITE permission
      if ($permarr["write"])
      {
          echo '&lt;br&gt;user can write: &lt;font color="green"&gt;TRUE&lt;/font&gt;';
      }
      else {
          echo '&lt;br&gt;user can write: &lt;font color="red"&gt;FALSE&lt;/font&gt;';
      }
  ?&gt;
  </code></pre>