Data::BitStream::Code::Comma - A Role implementing Comma codes


version 0.08


A role written for Data::BitStream that provides get and set methods for Comma codes. The role applies to a stream object.

Comma codes are described in many sources. The codes are written in k-bit chunks, where a chunk consisting of all 1 bits indicates the end of the code. The number to be encoded is stored in base 2^k-1. The case of 1-bit comma codes degenerates into unary codes. The most common comma code in current use is the ternary comma code which uses 2-bit chunks and stores the number in base 3 (hence why it is called ternary comma). Example for ternary comma:

      value        code          binary         bits
          0           c                    11    2
          1          1c                  0111    4
          2          2c                  1011    4
          3         10c                010011    6
          4         11c                010111    6
  ..      8         22c                101011    6
          9        100c              01000011    8
  ..     64       2101c            1001000111   10
  ..  10000  111201101c  01010110000101000111   20

Comma codes using larger chunks compact larger numbers better, but the terminator also grows. This means smaller values take more bits to encode, and all codes have many wasted bits after the information.

Also note that skipping the leading 0s for all codes results in a large waste of space. For instance, the codes 0xc, 0xxc, 0xxxc, etc. are all not used, even though they are uniquely decodable. Note that Fenwick's table 6 (p6) shows 0c being used, but no other leading zero. This is not the case in Sayood's table 3.19 (p71) where no entry has a leading zero.

These codes are a special case of the block-based taboo codes (Pigeon 2001). The taboo codes fully utilize all the bits.


Provided Object Methods

put_comma($bits, $value)
put_comma($bits, @values)

Insert one or more values as Comma codes using $bits bits. Returns 1.

get_comma($bits, $count)

Decode one or more Comma codes from the stream. If count is omitted, one value will be read. If count is negative, values will be read until the end of the stream is reached. In scalar context it returns the last code read; in array context it returns an array of all codes read.


The parameter bits must be an integer between 1 and 16. This indicates the number of bits used per chunk.

If bits is 1, then unary coding is used.

Ternary comma coding is the special case of comma coding with bits=2.

Byte coding is the special case of comma coding with bits=8.

Required Methods


These methods are required for the role.


Peter Fenwick, "Punctured Elias Codes for variable-length coding of the integers", Technical Report 137, Department of Computer Science, University of Auckland, December 1996.
Peter Fenwick, "Ziv-Lempel encoding with multi-bit flags", Proc. Data Compression Conference (IEEE DCC), Snowbird, Utah, pp 138-147, March 1993.
Khalid Sayood (editor), "Lossless Compression Handbook", 2003.


Dana Jacobsen <>


Copyright 2012 by Dana Jacobsen <>

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.