Skip to content

Commit ef8a89c

Browse files
committed
implement basic binary operators
1 parent 64b5463 commit ef8a89c

File tree

6 files changed

+186
-2
lines changed

6 files changed

+186
-2
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nbproject
2+
.idea
3+
vendor

composer.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "sokil/php-bitmap",
3+
"description": "Working with bits",
4+
"authors": [
5+
{
6+
"name": "Dmytro Sokil",
7+
"email": "dmytro.sokil@gmail.com"
8+
}
9+
],
10+
"require": {},
11+
"autoload": {
12+
"psr-4": {
13+
"Sokil\\": "src"
14+
}
15+
}
16+
}

src/Bitmap.php

+66-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,72 @@ public function __construct($bitmap = 0)
1111
$this->_bitmap = (int) $bitmap;
1212
}
1313

14-
public function isSet($index)
14+
public function isBitSet($index)
1515
{
16-
return 0 !== $this->_bitmap & (1 << $index);
16+
return (bool) ($this->_bitmap & (1 << $index));
17+
}
18+
19+
public function setBit($index)
20+
{
21+
$this->_bitmap = $this->_bitmap | (1 << $index);
22+
return $this;
23+
}
24+
25+
public function setBits(array $indexList)
26+
{
27+
$mask = 0;
28+
foreach($indexList as $index) {
29+
$mask = $mask | (1 << $index);
30+
}
31+
32+
$this->setBitsByMask($mask);
33+
34+
return $this;
35+
}
36+
37+
public function setBitsByMask($mask)
38+
{
39+
$this->_bitmap = $this->_bitmap | $mask;
40+
return $this;
41+
}
42+
43+
public function unsetBit($index)
44+
{
45+
$this->_bitmap = $this->_bitmap & ~(1 << $index);
46+
return $this;
47+
}
48+
49+
public function unsetBits(array $indexList)
50+
{
51+
$mask = 0;
52+
foreach($indexList as $index) {
53+
$mask = $mask | (1 << $index);
54+
}
55+
56+
$this->unsetBitsByMask($mask);
57+
58+
return $this;
59+
}
60+
61+
public function unsetBitsByMask($mask)
62+
{
63+
$this->_bitmap = $this->_bitmap & ~$mask;
64+
return $this;
65+
}
66+
67+
public function invert()
68+
{
69+
$this->_bitmap = ~$this->_bitmap;
70+
return $this;
71+
}
72+
73+
public function getInt()
74+
{
75+
return $this->_bitmap;
76+
}
77+
78+
public function getBinary()
79+
{
80+
return decbin($this->_bitmap);
1781
}
1882
}

tests/BitmapTest.php

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Sokil;
4+
5+
class BitmaskTest extends \PHPUnit_Framework_Testcase
6+
{
7+
public function testIsBitSet()
8+
{
9+
// 5 => 101
10+
$bitmap = new Bitmap(5);
11+
12+
$this->assertTrue($bitmap->isBitSet(2));
13+
}
14+
15+
public function testSetBit()
16+
{
17+
$bitmap = new Bitmap();
18+
19+
$this->assertEquals(8, $bitmap->setBit(3)->getInt());
20+
}
21+
22+
public function testSetBits()
23+
{
24+
$bitmap = new Bitmap();
25+
26+
$this->assertEquals(10, $bitmap->setBits(array(1, 3))->getInt());
27+
}
28+
29+
public function testSetBitsByMask()
30+
{
31+
$bitmap = new Bitmap();
32+
33+
$this->assertEquals(10, $bitmap->setBitsByMask(10)->getInt());
34+
}
35+
36+
public function testUnsetBit()
37+
{
38+
$bitmap = new Bitmap(5);
39+
40+
$this->assertEquals(4, $bitmap->unsetBit(0)->getInt());
41+
}
42+
43+
public function testUnsetBits()
44+
{
45+
$bitmap = new Bitmap(5);
46+
47+
$this->assertEquals(4, $bitmap->unsetBits(array(0, 1))->getInt());
48+
}
49+
50+
public function testUnsetBitsByMask()
51+
{
52+
$bitmap = new Bitmap(5);
53+
54+
$this->assertEquals(1, $bitmap->unsetBitsByMask(4)->getInt());
55+
}
56+
57+
public function testInvert()
58+
{
59+
$bitmap = new Bitmap(5);
60+
$bitmap->invert();
61+
62+
$this->assertEquals(-6, $bitmap->getInt());
63+
}
64+
65+
public function testGetInt()
66+
{
67+
$bitmap = new Bitmap();
68+
$bitmap->setBit(0);
69+
$bitmap->setBit(2);
70+
71+
$this->assertEquals(5, $bitmap->getInt());
72+
}
73+
74+
public function testBitmap()
75+
{
76+
$bitmap = new Bitmap();
77+
$bitmap->setBit(0);
78+
$bitmap->setBit(2);
79+
80+
$this->assertEquals('101', $bitmap->getBinary());
81+
}
82+
}

tests/bootstrap.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
/* @var $loader \Composer\Autoload\ClassLoader */
4+
$loader = require __DIR__ . "/../vendor/autoload.php";
5+
$loader->addPsr4('Sokil\\', __DIR__);
6+

tests/phpunit.xml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<phpunit colors="true">
3+
<testsuites>
4+
<testsuite name="AllTests">
5+
<directory>.</directory>
6+
</testsuite>
7+
</testsuites>
8+
<filter>
9+
<blacklist>
10+
<file>../src/Exception.php</file>
11+
</blacklist>
12+
</filter>
13+
</phpunit>

0 commit comments

Comments
 (0)