#binary-encoding #range #search #binary-search #bit-range #bit #domain #binary

no-std bbse

Backward Binary Search Encoding — minimal and deterministic scheme for sorted domains

4 releases

new 0.2.2 May 19, 2025
0.2.1 May 17, 2025
0.2.0 May 17, 2025
0.1.0 May 16, 2025

#283 in Compression

Download history 238/week @ 2025-05-12

238 downloads per month

MIT license

8KB
57 lines

bbse — Backward Binary Search Encoding

Crates.io

bbse is a minimal and deterministic encoding scheme for values in a sorted integer range.
It encodes a target value as a binary decision path, following the steps of binary search.

This approach provides a prefix-free, compact, and reversible representation of values
from any [start, end) interval.


✨ Features

  • Prefix-free binary encoding for any sorted range
  • Customizable midpoint for biased distributions
  • Simple, deterministic, and lossless
  • Suitable for compression, range indexing, embedded systems
  • No heap allocation (except for returned bitvector)

🚀 Example

use bbse::{encode, decode};

let bits = encode(0, 16, 5);        // e.g. [true, false, true]
let value = decode(0, 16, &bits);   // -> 5
assert_eq!(value, 5);

🎯 Custom midpoint

For skewed distributions (e.g., values near the beginning or end), you can control the first midpoint to reduce average path length:

use bbse::{encode_from, decode};

let bits = encode_from(0, 16, 3, 4);  // start=0, end=16, target=3, midpoint=4
let value = decode(0, 16, &bits);
assert_eq!(value, 3);

This gives you greater control over compression performance.


🎨 Use case: color encoding

The algorithm was originally inspired by the need to encode color deltas efficiently in an experimental lossless image format. Each delta channel (R/G/B) was encoded as a binary search path, taking advantage of sorted delta distributions.

This enabled compact per-channel encoding with minimal logic, without relying on entropy coding.


📦 Installation

[dependencies]
bbse = "0.2.0"

📄 License

MIT


🔮 Future directions

  • Optional no_std support for embedded targets

Dependencies

~1MB
~23K SLoC