Calculates high order bit mask for a word size
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 | |
function high_order_bitmask($wordSize) { | |
if($wordSize == 0 or $wordSize == 1){ | |
return 0; | |
} | |
$bitmask = 0; | |
for($i=$wordSize-1; $i >= $wordSize/2; $i-- ){ | |
$bitmask += pow(2,$i); | |
} | |
return $bitmask; | |
} | |
class HighOrderBitmaskSolution extends TestCase | |
{ | |
public function testTwoBits() { | |
$this->assertSame(2, high_order_bitmask(2)); | |
} | |
public function testFourBits() { | |
$this->assertSame(12, high_order_bitmask(4)); | |
} | |
public function testEightBits() { | |
$this->assertSame(240, high_order_bitmask(8)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment