Polecola.Primitive is a small .NET library for inspecting and manipulating the
bit representation of primitive values. It provides extension methods for
round-trip conversions between values, byte arrays, and Boolean arrays, plus
helpers for reading or changing individual bits and generating character or
floating-point ranges.
The library targets .NET Standard 2.1.
- Convert primitive values to
bool[]orbyte[]representations. - Reconstruct primitive values from Boolean or byte arrays.
- Read and change individual bits with
GetBoolAtIndexandSetBoolAtIndex. - Generate end-exclusive ranges for
char,float, anddouble. - Use the same extension-method API across signed, unsigned, integral, and floating-point values.
The project is currently consumed from source. Clone the repository and add a project reference from your application:
git clone https://github.com/MiLattanzio/Polecola.Primitive.git
dotnet add path/to/YourProject.csproj reference path/to/Polecola.Primitive/Polecola.Primitive/Polecola.Primitive.csprojThen import the namespace:
using Polecola.Primitive;Boolean arrays are least-significant-bit first within each byte, so index 0
represents the least significant bit.
using Polecola.Primitive;
byte value = 0b_0000_0101;
bool[] bits = value.ToBoolArray();
// [true, false, true, false, false, false, false, false]
bool bitTwo = value.GetBoolAtIndex(2); // true
byte updated = value.SetBoolAtIndex(1, true); // 0b_0000_0111
byte restored = bits.ToByte(); // 0b_0000_0101using Polecola.Primitive;
int original = 42;
byte[] bytes = original.ToByteArray();
int restored = bytes.ToInt();
bool[] representation = original.ToBoolArray();
int fromBits = representation.ToInt();Ranges include start and exclude end, like Enumerable.Range.
using Polecola.Primitive;
char[] letters = Polecola.Primitive.Char
.Range('a', 'f')
.ToArray(); // a, b, c, d, e
float[] samples = Polecola.Primitive.Float
.Range(0f, 1f, 0.25f)
.ToArray(); // 0, 0.25, 0.5, 0.75
double[] descending = Polecola.Primitive.Double
.Range(1d, 0d, -0.25d)
.ToArray(); // 1, 0.75, 0.5, 0.25Always provide an explicit, meaningful step for floating-point ranges. The
default is the type's Epsilon, which is usually too small to advance a value
at ordinary magnitudes.
- API guide describes the supported conversions, bit ordering, valid indices, range behavior, and portability considerations.
- XML documentation is emitted with the library assembly.
- A browsable API site can be generated with
Doxygen by running
doxygen Doxyfile; output is written toartifacts/docs/html. - Contributing guide contains the local build, test, and documentation workflow.
The repository requires the .NET 8 SDK or newer:
dotnet restore
dotnet build --no-restore
dotnet test --no-buildPolecola.Primitive is available under the MIT License.