Created
May 29, 2013 06:24
-
-
Save tot-ra/5668323 to your computer and use it in GitHub Desktop.
bit field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Manages variable as bitfield | |
*/ | |
class BitField { | |
private $value; | |
/** | |
* @param int $value initial value | |
*/ | |
public function __construct($value = 0) { | |
$this->value = $value; | |
} | |
/** | |
* @return int result value | |
*/ | |
public function getValue() { | |
return $this->value; | |
} | |
/** | |
* @param $n | |
* | |
* @return bool | |
*/ | |
public function get($n) { | |
return ($this->value & (1 << $n)) != 0; | |
} | |
public function set($n, $new = true) { | |
$this->value = ($this->value & ~(1 << $n)) | ($new << $n); | |
} | |
public function clear($n) { | |
$this->set($n, false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment