Binary primitives for .NET

Bits at machine speed.
Formats at wire precision.

Manipulate integer bits, pack arbitrary-width fields, and encode compact variable-length values through one dependency-free library.

PacketWriter.cs
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

Small operations.
Complete coverage.

Every mutating operation has an in-place form and a copy-returning counterpart. Supported CPUs use runtime intrinsics; other platforms use equivalent software implementations.

Transform

Set, clear, toggle, rotate, reverse, or change endianness across every integral width.

Extract

Pack and unpack ranges, isolate set bits, clear high or low regions, and apply PEXT or PDEP semantics.

Copy or mutate

Choose direct ref mutation or a Copy method that preserves the original value.

Z

ZigZag

Fold signed integers into unsigned values so small magnitudes retain compact representations.

CPU

Hardware-aware

POPCNT, LZCNT, TZCNT, BMI1, BMI2, BSWAP, and ARM64 RBIT are selected when available.

Span-based binary formats

Cross byte boundaries without allocations.

BitPacking, BitWriter, and BitReader operate over caller-owned spans. Fields may start at any bit offset and range from zero to 64 bits.

  • MSB-first fields for common network and media layouts
  • Read-modify-write preserves neighboring fields
  • Try variants report malformed or undersized input without throwing
  • Windowed core reads a field in one 64-bit load, not one byte per iteration

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.

ULEB128Unsigned variable integersDWARF · WebAssembly · Protocol Buffers
SLEB128Signed variable integersTwo's-complement sign extension
ZIGZAGSigned magnitude foldingProtocol Buffers sint semantics
EBML VINTRFC 8794 integersMatroska · WebM
SIZE PREFIXOne-to-four byte lengthsUp to 30 payload bits

One vocabulary

Use the same API across integer widths.

Methods cover sbyte, byte, short, ushort, int, uint, long, and ulong.

IsBitSetPopCountParityRol / RorReverseBitsReverseEndiannessLeadingZeroCountTrailingZeroCountRoundUpToPowerOf2ParallelBitExtractParallelBitDepositToBitString

NuGet package

Add one package.
Bring your own buffers.

Tedd.BitUtils targets .NET Standard 2.1, .NET 6, .NET 8, and .NET 10 under LGPL-3.0.

TERMINAL dotnet add package Tedd.BitUtils