navarr / binary-explode
Explode a number into it's binary components
Fund package maintenance!
navarr
Installs: 4 717
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 1
Open Issues: 0
Requires
- php: ^7.1|^8
Requires (Dev)
- infection/infection: ^0.25.0
- jetbrains/phpstorm-attributes: ^1.0
- phpstan/phpstan: ^1
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^3.6
README
This package provides two functions usable to convert a number into an array of it's binary components.
Examples:
- [1]
- [2]
- [2, 1]
- [4]
- [4, 1]
- [4, 2]
- [4, 2, 1]
etc
Usage
Installation
composer require navarr/binary-explode`
Shared Interface
This package provides a shared interface, \Navarr\BinaryExploder\BinaryExploderInterface
. This interface takes an int
and returns an iterable<int>
containing or generating the binary components of that int
.
\Navarr\BinaryExploder\LinearBinaryExploder::explode(int $value) : iterable
Uses a linear algorithm that checks the logical AND of the number with increasing powers of 2.
This algorithm is typically fastest for numbers that are unlikely to be a power of 2.
Navarr\BinaryExploder\LogarithmicBinaryExploder::explode(int $value) : iterable
Uses a linear algorithm that checks the log(base 2) of the number and then it's remainders.
This algorithm is typically fastest for numbers that are likely to be a power of 2.