# jon@xxxxxxxxxxxxxxxxxx / 2007-01-25 14:34:51 -0500: > function bits($num) { > $bit_array = str_split(strrev(decbin(intval($num)))); > $val_array = array(); > foreach ($bit_array as $pow => $bit) { > if ($val = $bit * pow(2,$pow)) > $val_array[] = $val; > } > return($val_array); > } > > (I wanted to see if I could write it in few LOC.) I wonder if there's a > faster way... I didn't time either version, and I'm no mathematician either, so this is prolly a stupid solution. <?php function bitarray($ored) { $rv = array(); for ($v = 1; $v <= $ored; $v *= 2) { if ($ored & $v) { array_push($rv, $v); } } return $rv; } class SingleBitTest extends Tence_TestCase { private function doTest($int) { return $this->assertEquals( array($int), bitarray($int) ); } function testE_ERROR() { return $this->doTest(E_ERROR); } function testE_WARNING() { return $this->doTest(E_WARNING); } function testE_NOTICE() { return $this->doTest(E_NOTICE); } function testE_USER_ERROR() { return $this->doTest(E_USER_ERROR); } function testE_USER_WARNING() { return $this->doTest(E_USER_WARNING); } function testE_USER_NOTICE() { return $this->doTest(E_USER_NOTICE); } } class BitArrayTest extends Tence_TestCase { private function doTest(array $expected, $int) { return $this->assertEquals( $expected, bitarray($int) ); } function test7() { return $this->doTest(array(1, 2, 4), 7); } function test8() { return $this->doTest(array(8), 8); } function testERROR_WARNING_NOTICE_STRICT() { return $this->doTest( array(E_ERROR, E_WARNING, E_NOTICE, E_STRICT), E_ERROR|E_WARNING|E_NOTICE|E_STRICT ); } } class bttests extends Tence_TestSuite { function __construct() { $this ->add(new SingleBitTest) ->add(new BitArrayTest) ; } } ?> -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php