Abit - functions to work with :atomics as a bit array (?)

I have extracted this small package from one of my hobby projects.
If anybody is comfortable with bitmasks or Elxir.Bitwise it would be cool to receive some feedback on this.

I’m not sure about a few naming and the complexity of the code when I’m setting a bit with Abit.set_bit/3.

GitHub: https://github.com/preciz/abit
Hexdocs: https://hexdocs.pm/abit

It’s main use case is if you use an :atomics array as a bit array.

3 Likes

I have added a module Abit.Counter.
This is similar to Erlang :counters but it allows you to use counters with smaller than 64 bit sizes:

bits | unsigned value range | signed value range
2      0..3                   -2..1
4      0..15                  -8..7
8      0..255                 -128..127
16     0..65535               -32768..32767
32     0..4294967295          -2147483648..2147483647

So for example if you only need max value 255 (8 bits) unsigned, you can have atomics_size * 8 counters instead of just atomics_size.

Here is the short summary of the API of Abit.Counter module:

Abit.Counter

  • Abit.Counter.new/2 - Create a new array of counters. Returns %Abit.Counter{} struct.
  • Abit.Counter.get/2 - Returns the value of counter at the given index.
  • Abit.Counter.put/3 - Puts the value into counter at the given index.
  • Abit.Counter.add/3 - Adds the increment to counter at the given index.
1 Like