Skip to content

Instantly share code, notes, and snippets.

@hadaytullah
Last active October 25, 2019 09:18
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 hadaytullah/7d26bb40dc5b8c4334e5d2af6d14aa4f to your computer and use it in GitHub Desktop.
Save hadaytullah/7d26bb40dc5b8c4334e5d2af6d14aa4f to your computer and use it in GitHub Desktop.
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