Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculates high order bit mask for a word size
<? 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