php - Calculate the index number of a value - bitwise sort of -


i want provide user visibility subscription pages using bitwise operators . if user's permission page 60 able see subscription pages visibility set on 2,3,4,5 (eg. 2^2 =4,2^3=8,2^4=16,2^5=32 32+16+4+2 =60 allowed view pages permission id =2,3,4,5)

   public function permission($perm)    {      $this->permission = ~$perm & $this->permission;    } 

i used code useless need , suggesations.......thank :)

adding permission:

$this->permission |= $perm; 

checking if permission set:

$is_permitted = (bool)($this->permission & $perm); 

checking permissions set:

for ($permissions = array(), $i = ceil(log($this->permission, 2) + 0.1) /* floating point imprecision */; $i >= 0; $i--)     if ($this->permission & (1 << $i))         $permissions[] = $i; 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -