Skip to content

Instantly share code, notes, and snippets.

@tot-ra
Created May 29, 2013 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tot-ra/5668323 to your computer and use it in GitHub Desktop.
Save tot-ra/5668323 to your computer and use it in GitHub Desktop.
bit field
<?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