Created
August 6, 2014 19:41
-
-
Save triple-j/71da70208c3af6fd33de to your computer and use it in GitHub Desktop.
Enable/Disable/View Bitwise Flags
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 | |
/** | |
* @desc Enable/Disable/View Bitwise Flags | |
* (Useful when you need a group of true/false values) | |
* @author Jeremie J. Jarosh | |
* | |
* NOTE: slightly modifyed from code provided by 'wbcarts at juno dot com' | |
* (http://www.php.net/manual/en/language.operators.bitwise.php#108679) | |
*/ | |
class BitwiseFlag { | |
public $flags; | |
public function __construct( $value=0 ) { | |
$this->flags = $value; | |
} | |
public function getFlag( $digit ) { | |
$place = 1 << $digit; | |
return (($this->flags & $place) == $place); | |
} | |
public function setFlag($digit, $value) { | |
$place = 1 << $digit; | |
if ( $value == true ) { | |
// enable the flag | |
$this->flags |= $place; | |
} else { | |
// disable the flag | |
$this->flags &= ~$place; | |
} | |
} | |
public function __ToString() { | |
return decbin( $this->flags ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment