Project

bit_utils

0.0
No commit activity in last 3 years
No release in over 3 years
Utility methods for manipulating bits within Integer.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
~> 10.0
~> 3.0

Runtime

>= 3.6.6
 Project Readme

BitUtils

Build Status

Utility methods for bit-wise operations of Integer.

Installation

Add this line to your application's Gemfile:

gem 'bit_utils'

And then execute:

$ bundle

Or install it yourself as:

$ gem install bit_utils

Usage

Module BitUtils appears after require 'bit_utils'.

Currently BitUtils offers three functionalities.

BitUtils.#popcount(num)

Returns popcount (Hamming weight) of the num; i.e. equal to num.to_s(2).count('1') for non-negative numbers.

for negative numbers

Theoretically, Ruby treats negative integers as if infinite 1-bits are leaded (printf '%x', -1 gives ..f ), so counting 1 bits for negative numbers is meaningless.

In this implementation, popcount for negative number counts 0 bits and negates the result. (distinguishing with positive version) Same in Ruby:

return -popcount(~num) if num < 0

Note that both popcount(0) and popcount(-1) still return 0.

BitUtils.#trailing_zeros(num)

Returns the number of 0 bits continuing from least significant bit. If num == 0, it returns -1.

BitUtils.#each_bit(num, &block)

Iterate over 1-bit positions in Integer. If block is not given, it returns Enumerator.

The yielding order is unspecified; may change by platform/version.

If negative num is given, it raises RangeError

common to all functionalities

All of the module functions raise TypeError unless the parameter is Integer.

All module functions have .#xxx_fixnum and .#xxx_bignum (specific for Fixnum and Bignum), but these are not intended for direct use; these will be removed in the future..

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. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Limitations

This gem is mainly developed on x64 GCC environment, so in other environments it may not work or be slow.

Pull requests for other environments are welcome.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jkr2255/bit_utils.

License

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