php - What's the purpose of this bitwise AND operator? -
the code basic php code cms,it read mysql content render web page based on 3 different methods ,by id,name or special type(index page,sitemap page ...).but can't understand line " $r=dbrow("select * pages special&$v limit 1");" bitwise , supposed ?
<?php class page{ static $instances = array(); static $instancesbyname = array(); static $instancesbyspecial = array(); function __construct($v,$byfield=0,$fromrow=0,$pvq=0){ # byfield: 0=id; 1=name; 3=special if (!$byfield && is_numeric($v)){ // id $r=$fromrow?$fromrow:($v?dbrow("select * pages id=$v limit 1"):array()); } else if ($byfield == 1){ // name $name=strtolower(str_replace('-','_',$v)); $fname='page_by_name_'.md5($name); $r=dbrow("select * pages name '".addslashes($name)."' limit 1"); } else if ($byfield == 3 && is_numeric($v)){ // special $fname='page_by_special_'.$v; $r=dbrow("select * pages special&$v limit 1"); } else return false; if(!count($r || !is_array($r)))return false; if(!isset($r['id']))$r['id']=0; if(!isset($r['type']))$r['type']=0; if(!isset($r['special']))$r['special']=0; if(!isset($r['name']))$r['name']='no name supplied'; foreach ($r $k=>$v) $this->{$k}=$v; $this->urlname=$r['name']; $this->dbvals=$r; self::$instances[$this->id] =& $this; self::$instancesbyname[preg_replace('/[^a-z0-9]/','-',strtolower($this->urlname))] =& $this; self::$instancesbyspecial[$this->special] =& $this; if(!$this->vars)$this->vars='{}'; $this->vars=json_decode($this->vars); } function getinstance($id=0,$fromrow=false,$pvq=false){ if (!is_numeric($id)) return false; if (!@array_key_exists($id,self::$instances)) self::$instances[$id]=new page($id,0,$fromrow,$pvq); return self::$instances[$id]; } function getinstancebyname($name=''){ $name=strtolower($name); $nameindex=preg_replace('#[^a-z0-9/]#','-',$name); if(@array_key_exists($nameindex,self::$instancesbyname))return self::$instancesbyname[$nameindex]; self::$instancesbyname[$nameindex]=new page($name,1); return self::$instancesbyname[$nameindex]; } function getinstancebyspecial($sp=0){ if (!is_numeric($sp)) return false; if (!@array_key_exists($sp,$instancesbyspecial)) $instancesbyspecial[$sp]=new page($sp,3); return $instancesbyspecial[$sp]; } }
it appears pages.special
contains bit field (that is, each bit position in values flag application-specific property).
then, find records have properties, 1 perform bitwise and
operation desired "mask"—bit positions in result set only if same position set in both value , mask. complete truth-table 2-bit value this:
+---------+------+------------+ | value | mask | value&mask | +---------+------+------------+ | 0b00 | 0b00 | 0b00 | | 0b01 | 0b00 | 0b00 | | 0b10 | 0b00 | 0b00 | | 0b11 | 0b00 | 0b00 | | 0b00 | 0b01 | 0b00 | | 0b01 | 0b01 | 0b01 | | 0b10 | 0b01 | 0b00 | | 0b11 | 0b01 | 0b01 | | 0b00 | 0b10 | 0b00 | | 0b01 | 0b10 | 0b00 | | 0b10 | 0b10 | 0b10 | | 0b11 | 0b10 | 0b10 | | 0b00 | 0b11 | 0b00 | | 0b01 | 0b11 | 0b01 | | 0b10 | 0b11 | 0b10 | | 0b11 | 0b11 | 0b11 | +---------+------+------------+
for example, suppose lowest-order bit flag respective page "is readonly"; , next bit page "requires moderation"—a value of (decimal) 2, or 0b10
, indicate page not readonly does require moderation. so, find pages either readonly or require moderation, 1 do:
select * pages (special & 0b11) != 0
because mysql doesn't have true boolean types (but instead treats 0 false , non-zero true), filter expression can abbreviated:
select * pages special & 0b11
we state mask in decimal, rather binary, form:
select * pages special & 3
and, if desired, apply limit
query (although in absence of order by
clause, result indeterminate):
select * pages special&3 limit 1
given have work (from code have shown), can following:
the query selects columns of indeterminate record pages
table value of special
has @ least 1 of same bits set in mask $v
.
since semantics of special
, $v
(and bit positions within them) application-specific, impossible 1 more without deeper understanding of application.
note that, whilst compact, filtering using mask on bit field not sargable—so tends poor database design.
Comments
Post a Comment