utilities for dealing with packed bitfields
Perl 6
Switch branches/tags
Latest commit ef376f9 Nov 4, 2017 @jonathanstowe jonathanstowe Add changes
Failed to load latest commit information.
lib/Util Tidy Up Aug 14, 2017
t Add META test Aug 14, 2017
.gitignore Start the module Mar 5, 2016
.travis.yml Alter travis config to use zef Aug 2, 2017
Changes Add changes Nov 4, 2017
LICENCE rename LICENCE as en_GB May 30, 2016
META6.json Tidy Up Aug 14, 2017
README.md Tidy Up Aug 14, 2017



Utility subroutines for working with bitfields

Build Status


use Util::Bitfield;

my $number = 0b0001011101101010;

# source integer, number of bits, starting position, word size
say extract-bits($number,3,3,16); # 5
say sprintf "%016b",insert-bits(7, $number, 3, 3, 16); # "0001111101101010"


"Bitfields" are common in hardware interfaces and compact binary data formats, allowing the packing of multiple fields of information within a single machine word sized value for instance, hardware examples might include device registers or gpio ports, software examples include MP3 "frame headers".

Whilst highly efficient for data storage and transmission, they're usually a pain to work with in high level languages, requiring masking and shifting of numbers possibly multiple times to get a value you can sensibly use in your program.

Also because it's not something I at least tend to do very frequently the patterns don't come naturally and I end up starting from first principles every time.

So to this end, on being presented with some data that required unpacking of a bit field, I made this fairly simple library to extract and insert an arbitrary number of bits from an arbitrary location within a larger integer as smaller integers.


Assuming you have a working Rakudo Perl 6 installation you should be able to install this with zef :

# From the source directory

zef install .

# Remote installation

zef install Util::Bitfield

Other install mechanisms may be become available in the future.


Suggestions/patches are welcomed via github at



This is free software.

Please see the LICENCE file in the distribution

© Jonathan Stowe 2016, 2017