There is some code as part of the prototype. It might make sense to use bitarray (or a similar package) to take advantage of an optimised implementation.
It might make sense to wrap this into a class that provides a string-like interface to make working with it easier.