BitBuffer.php 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Class BitBuffer
  4. *
  5. * @filesource BitBuffer.php
  6. * @created 25.11.2015
  7. * @package chillerlan\QRCode
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2015 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode;
  13. /**
  14. *
  15. */
  16. class BitBuffer{
  17. /**
  18. * @var array
  19. */
  20. public $buffer = [];
  21. /**
  22. * @var int
  23. */
  24. public $length = 0;
  25. /**
  26. *
  27. */
  28. public function clear(){
  29. $this->buffer = [];
  30. $this->length = 0;
  31. }
  32. /**
  33. * @param int $num
  34. * @param int $length
  35. *
  36. * @return $this
  37. */
  38. public function put($num, $length){
  39. for($i = 0; $i < $length; $i++){
  40. $this->putBit(($num >> ($length - $i - 1))&1 === 1);
  41. }
  42. }
  43. /**
  44. * @param bool $bit
  45. *
  46. * @return $this
  47. */
  48. public function putBit($bit){
  49. $bufIndex = floor($this->length / 8);
  50. if(count($this->buffer) <= $bufIndex){
  51. $this->buffer[] = 0;
  52. }
  53. if($bit){
  54. $this->buffer[(int)$bufIndex] |= (0x80 >> ($this->length % 8));
  55. }
  56. $this->length++;
  57. }
  58. }