NAME
Bit::Twiddling - Low-level bit-twiddling hacks
VERSION
Version 0.10
SYNOPSIS
use Bit::Twiddling 'count_set_bits';
my $number = 0b1111_0001;
my $set_bits = count_set_bits($number); # 5
use Bit::Twiddling 'nearest_higher_power_of_2';
print nearest_higher_power_of_2(   0); # 1
print nearest_higher_power_of_2(1000); # 1024
print nearest_higher_power_of_2(1024); # 1024
DESCRIPTION
This library is a collection of bit-manipulation functions written in C, all taken from the Bit Twiddling Hacks webpage.
EXPORTS
Nothing is exported by default, but the following functions are available for export by name
count_set_bits
nearest_higher_power_of_2
Additionally, you can request the export of all functions by use Bit::Twiddling ':all'.
FUNCTIONS
The functions in this module expect a single integer argument, but will convert string to numeric if needed (and give an Argument "..." isn't numeric in subroutine entry warning). If the argument is undef, it will be treated as if it were zero and generate a Use of uninitialized value in subroutine entry warning.
This distribution was designed to work with a perl compiled with use64bitint and uselongdouble. It should, however, be OK without these options.
count_set_bits
$bits = count_set_bits($number);
count_set_bits will return the count of how many bits are set (1) in the binary representation of $number. $number is assumed to be compatible with C's long int type (probably 64-bits).
nearest_higher_power_of_2
$power_of_2 = nearest_higher_power_of_2($number);
nearest_higher_power_of_2 will return the largest power-of-two that is greater-than-or-equal-to $number.
EXAMPLES
There are two scripts in the examples folder of the dist.
c.pl
This script contains the original C code that was used with Inline::C to generate the module's XS.
benchmarks.pl
Some benchmarks of this module versus various pure Perl implementations.
AUTHOR
Brian Greenfield <briang at cpan dot org>
REPORTING BUGS & OTHER WAYS TO CONTRIBUTE
The code for this module is maintained on GitHub.
If you have a patch, feel free to fork the repository and submit a pull request. If you find a bug, please open an issue on the project at GitHub (https://github.com/briang/p5-bit-twiddling/issues).
ACKNOWLEDGEMENTS
* Steve Bertrand's Wrapping a C shared library with Perl and XS tutorial
LICENSE AND COPYRIGHT
Copyright 2018 Brian Greenfield
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.