Skip to main content

shannon_entropy

Function shannon_entropy 

Source
pub fn shannon_entropy(data: &[u8]) -> f32
Expand description

Compute Shannon entropy of a byte slice, returning bits per symbol (0.0-8.0).

Single-pass O(n) time, O(1) space (256-entry frequency table). Returns 0.0 for empty input.

Reference ranges:

  • English text: ~4.0-5.5 bits/byte
  • Compiled binaries / structured data: ~5.0-7.0 bits/byte
  • Compressed data (zstd, gzip): ~7.5-7.9 bits/byte
  • Encrypted / random data: ~7.95-8.0 bits/byte

Used by the copy pipeline to classify chunk content (e.g. skip compression on already-compressed or encrypted data).