作者:mobiledu2502863997 | 来源:互联网 | 2018-03-07 22:36
<&#63;php
//所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素。由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省。
/*
若 N =1 ; 申请内存空间为 int a[2] ;
假设需要排序或者查找的总数N=10000000,那么我们需要申请内存空间的大小为int a[1 + N/32],其中:a[0]在内存中占32为可以对应十进制数0-31,依次类推:
1.求十进制0-N对应在数组a中的下标: n/32
2.求0-N对应0-31中的数: N%32=M
3.利用移位0-31使得对应32bit位为1: 1<>SHIFT 这里相当于 intval($i /32) ;
// $i & $this->mask 这里相当于 $i % $this->mask ,取余
@$this->bitArray[$i >> $this->shift] &= ~(1<<($i & $this->mask));
}
/**
$i 对应的数致1
*/
function setbit($i){
@$this->bitArray[$i >> $this->shift] |= (1<<($i & $this->mask));
}
//test 测试所在的bit为是否为1
function testbit($i){
return $this->bitArray[$i >> $this->shift] & (1<<($i & $this->mask));
}
}
$oBig = new bigMap() ;
$oBig->setbit(30) ;
var_dump($oBig->testbit(2)) ;
var_dump($oBig->bitArray) ;
echo decbin($oBig->bitArray[0]),"
";