All pastes #128057 Raw Edit

Stuff

public php v1 · immutable
#128057 ·published 2006-08-12 17:38 UTC
rendered paste body
<?phpclass bitfield{	var $data;	function bitfield($bitfield = '')	{		$this->data = base64_decode($bitfield);	}	/**	*/	function get($n)	{		// Get the ($n / 8)th char		$byte = $n >> 3;		if (!isset($this->data[$byte]))		{			// Of course, if it doesn't exist then the result if FALSE			return false;		}		$c = $this->data[$byte];		// Lookup the ($n % 8)th bit of the byte		$bit = 7 - ($n & 7);		return (bool) (ord($c) & (1 << $bit));	}	function set($n)	{		$byte = $n >> 3;		$bit = 7 - ($n & 7);		if (isset($this->data[$byte]))		{			$this->data[$byte] = $this->data[$byte] | chr(1 << $bit);		}		else		{			if ($byte - strlen($this->data) > 0)			{				$this->data .= str_repeat("\0", $byte - strlen($this->data));			}			$this->data .= chr(1 << $bit);		}	}	function clear($n)	{		$byte = $n >> 3;		if (!isset($this->data[$byte]))		{			return;		}		$bit = 7 - ($n & 7);		$this->data[$byte] = $this->data[$byte] &~ chr(1 << $bit);	}	function get_blob()	{		return $this->data;	}	function get_base64()	{		return base64_encode($this->data);	}	function get_bin()	{		$bin = '';		$len = strlen($this->data);		for ($i = 0; $i < $len; ++$i)		{			$bin .= str_pad(decbin(ord($this->data[$i])), 8, '0', STR_PAD_LEFT);		}		return $bin;	}	function get_all_set()	{		return array_keys(array_filter(str_split($this->get_bin())));	}	function merge($bitfield)	{		$this->data = $this->data | $bitfield->get_blob();	}}$bitfield = new bitfield();$bitfield->set(0);$bitfield->set(3);$bitfield->set(8);$bitfield->set(9);$bitfield->set(11);$bitfield->set(12);var_dump($bitfield->get_base64());?>