Next, PHP Bitset module Introduction and Installation
The Bitset module of PHP can be used to realize the Bitset related functions of C + +. However, it is dangerous and troublesome to install modules on the online server in a production environment, so it is necessary to make a further alternative.
I use an array of PHP arrays to simulate the implementation of several main methods of bitset, in fact, using the key of the array to simulate memory address, value modulo address content. The PHP array is also super handy:)
1<?PHP2 Define(' Char_bit ', 8);3 /**4 * Bitset operation PHP Implementation5 * @version: 1.06 * @author: Kenny{kenny.f<mailto:[email protected]>}7 * @since: 2014/05/218 */9 classBitset {Ten One Private $bitset _data=Array(); A Private $_len= 0; - - //allocating bit array space the function&bitset_empty ($bit=0) - { - if(!Is_numeric($bit) ||$bit<0) - { + Echo"Argument must be a positive integer"; - return False; + } A $this->_len =$bit; at return $this-Bitset_data; - } - - //bit array position value on $bit is set to 1 - Public functionBITSET_INCL (&$bitset _data=Array(),$bit=0) - { in if(!Is_numeric($bit) ||$bit<0) - { to Echo"Second argument must be a positive integer"; + return False; - } the * $bitset _temp=isset($bitset _data[intval($bit/char_bit)])?$bitset _data[intval($bit/char_bit)]: 0; $ $bitset _data[intval($bit/char_bit)] =$bitset _temp| 1 << ($bit%char_bit);Panax Notoginseng - unset($bitset _data); the } + A //determine if the value on bit in a location is 1 the Public functionBitset_in ($bitset _data=Array(),$bit=0) + { - if(!Is_array($bitset _data)) $ { $ Echo"First argument is not a array"; - return False; - } the - if($bit< 0)Wuyi { the return False; - } Wu if($this->_len = = 0) - { About return False; $}ElseIf($bit>=$this->_len*char_bit) { - return False; -}ElseIf($bitset _data[intval($bit/char_bit)] & (1 << ($bit%char_bit))) { - return True; A}Else{ + return False; the } - } $ the}
You can view my GitHub
Using PHP array to implement BITSET bit processing module function