# ExPopcount
[](https://github.com/tsutsu/ex_popcount/actions/workflows/ci.yml)
Popcount, or population count, is a function that determines the number of set bits (bits with a value of 1) in a binary representation of an integer.
ExPopcount is an Erlang NIF library that exposes a `popcount/1` operation over arbitrary integers and binaries/bitstrings.
The performance of the underlying operation relies mostly on the performance of the Rust stdlib `u64::count_ones` operation.
## Installation
The package can be installed by adding `ex_popcount` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:ex_popcount, "~> 0.1.0"}
]
end
```
### Force compilation
This library includes pre-compiled binaries for the native Rust code. If you
want to force-compile the Rust code, you can add the following configuration
to your application:
```
config :rustler_precompiled, :force_build, ex_popcount: true
```
You also need to add Rustler to your dependencies:
```
def deps do
[
{:ex_popcount, "~> 0.1.0"},
{:rustler, ">= 0.0.0", optional: true}
]
end
```
## Usage
To calculate a popcount for an arbitrary value, use `ExPopcount.popcount/1` function:
```elixir
4 = ExPopcount.popcount(0b1111)
4 = ExPopcount.popcount(<<0b1111>>)
4 = ExPopcount.popcount(<<0::32, 0b1111::4>>)
```
`popcount/1` returns `ArgumentError` if the provided data is not an integer or bitstring, or if the provided data is a negative integer.
## Contributing
1. [Fork it!](https://github.com/tsutsu/ex_popcount)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
## License
ExPopcount is released under the MIT License.