-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBitBuffer.php
More file actions
55 lines (42 loc) · 1.05 KB
/
Copy pathBitBuffer.php
File metadata and controls
55 lines (42 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace GlobusStudio\QRCode\Encoder;
final class BitBuffer
{
/** @var int[] */
private array $buffer = [];
private int $length = 0;
/**
* @return int[]
*/
public function getBuffer(): array
{
return $this->buffer;
}
public function getLengthInBits(): int
{
return $this->length;
}
public function get(int $index): bool
{
$bufIndex = (int) ($index / 8);
return (($this->buffer[$bufIndex] >> (7 - $index % 8)) & 1) === 1;
}
public function put(int $num, int $length): void
{
for ($i = 0; $i < $length; $i++) {
$this->putBit((($num >> ($length - $i - 1)) & 1) === 1);
}
}
public function putBit(bool $bit): void
{
$bufIndex = (int) ($this->length / 8);
if (count($this->buffer) <= $bufIndex) {
$this->buffer[] = 0;
}
if ($bit) {
$this->buffer[$bufIndex] |= (0x80 >> ($this->length % 8));
}
$this->length++;
}
}