Inspect
Test bits, count population and parity, locate leading and trailing zeros, and calculate bit length or integer log₂.
Binary primitives for .NET
Manipulate integer bits, pack arbitrary-width fields, and encode compact variable-length values through one dependency-free library.
Span<byte> buffer = stackalloc byte[4];
var writer = new BitWriter(buffer);
writer.Write(0b101, 3);
writer.Write(0xABC, 12);
writer.AlignToByte();
var packet = writer.WrittenSpan;
8 integer typesSigned and unsigned
4 target frameworks.NET Standard 2.1 to .NET 10
0 dependenciesOn every target
Automatic fallbackIntrinsic or portable path
Integer operations
Every mutating operation has an in-place form and a copy-returning counterpart. Supported CPUs use runtime intrinsics; other platforms use equivalent software implementations.
Test bits, count population and parity, locate leading and trailing zeros, and calculate bit length or integer log₂.
Set, clear, toggle, rotate, reverse, or change endianness across every integral width.
Pack and unpack ranges, isolate set bits, clear high or low regions, and apply PEXT or PDEP semantics.
Choose direct ref mutation or a Copy method that preserves the original value.
Fold signed integers into unsigned values so small magnitudes retain compact representations.
POPCNT, LZCNT, TZCNT, BMI1, BMI2, BSWAP, and ARM64 RBIT are selected when available.
Span-based binary formats
BitPacking, BitWriter, and BitReader operate over caller-owned spans. Fields may start at any bit offset and range from zero to 64 bits.
Measured
Packing and parsing a sixteen-field mixed-width packet through BitWriter and BitReader is 1.15× faster to write and 1.18× faster to read than the previous byte-at-a-time core. Individual wide fields gain more: a 64-bit aligned field is 5.7× faster to read and 6.5× to write.
AMD Ryzen 9 5950X, .NET 8, BenchmarkDotNet. Narrow fields of eight bits or fewer are unchanged within the harness’s resolution. Benchmarks ship in the repository.
One vocabulary
Methods cover sbyte, byte, short, ushort, int, uint, long, and ulong.
NuGet package
Tedd.BitUtils targets .NET Standard 2.1, .NET 6, .NET 8, and .NET 10 under LGPL-3.0.
dotnet add package Tedd.BitUtils