Using PHP array to implement BITSET bit processing module function

Source: Internet
Author: User
Tags bitset

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.