Skip to content

Instantly share code, notes, and snippets.

@triple-j
Created August 6, 2014 19:41
Show Gist options
  • Save triple-j/71da70208c3af6fd33de to your computer and use it in GitHub Desktop.
Save triple-j/71da70208c3af6fd33de to your computer and use it in GitHub Desktop.
Enable/Disable/View Bitwise Flags
<?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