Bitfifo

Bitfifo implements a FIFO for 1 or more bits of an Integer. This will be needed for a compression and encryption system. Could be used for any variable length integer based problems including base64 encode/decode. Includes bit reversals for LSB first or MSB first systems. No need to worry about overfill as a Bignum is used to store bits.

Installation

Add this line to your application's Gemfile:

gem 'bitfifo'

And then execute:

$ bundle

Or install it yourself as:

$ gem install bitfifo

Usage

A FIFO (first in first out) for variable length integers is a necessary component for data compression systems. Basically, what you put is what you get in the same order. See the example below:


# single bits:
fifo = Bitfifo.new
fifo.put true       # converts true/false to 1/0
fifo.get            # returns 1

# multiple bits:
fifo = Bitfifo.new
fifo.put 99, 7      # store 7 bits of the number 99
fifo.put 3,5        # store 5 bits of the number 3
fifo.put 0xffff     # stores lsb bit only, others masked
fifo.count          # >> 13  the number of stored bits
fifo.get 7          # >> 99
fifo.get 5          # >> 3

# reverse bits:
fifo = Bitfifo.new
fifo.rput 0b11010101, 8  # store 213 in reverse bit order
fifo.get 8               # >> 0b10101011 or 171
Bitfifo.reverse(171,8)   # >> 213

# what you reverse put, you reverse get
fifo = Bitfifo.new
fifo.rput 99, 7      # store 7 bits of the number 99
fifo.rput 3,5        # store 5 bits of the number 3
fifo.rget 7          # >> 99
fifo.rget 5          # >> 3

The current methods are as follows:

# instance methods:
#count                 ... returns the number of stored bits
#size                  ... same thing as count
#put(data, nbits=nil)  ... stores `nbits` of data or a single bit if `nbits` not specified
#rput(data, nbits=nil) ... reverses bit order of data, then calls #put
#get(nbits=1)          ... retrieves nbits of data
#rget(nbits=1)         ... retrieves nbits of data in reverse bit order
#unget(nbits=1)        ... put back data from last #get or #rget. The next #get will return the same data.
#to_i                  ... returns internal number with size mask
#to_i(false)           ... returns internal number without size mask
#empty?                ... true if there is no data in FIFO
#empty                 ... clears all data from FIFO, returns what was cleared.   

# class method:
# Bitfifo.reverse(data, nbits) ... returns reversed bit order of nbits worth of data. Other bits are masked.

Revision History

Version 0.2.1

  • fixed rget on empty fifo bug

Version 0.2.0

  • added method #unget
  • added method #to_i

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

I need to control this for the time being

License

The gem is available as open source under the terms of the MIT License.