bithacks 1.0.0 copy "bithacks: ^1.0.0" to clipboard
bithacks: ^1.0.0 copied to clipboard

Collection of bit twiddling hacks. Think _popcnt64 or _pdep_u64.

bithacks #

by Andrew Brampton (bramp.net)

Collection of bit twiddling hacks. Think _popcnt64 or _pdep_u64.

GitHub | Package | API Docs

Usage #

  // Count the number of bits set.
  0.bitCount(); // returns 0
  1.bitCount(); // returns 1
  2.bitCount(); // returns 1
  3.bitCount(); // returns 2
  0x10101010.bitCount(); // returns 4

  // Find the position of set bits. For example:
  //
  //   7 6 5 4 3 2 1 0 (index)
  //   0 1 0 0 0 0 1 1 (value 0x43 with 3 bits set)
  //     2         1 0 (rank)
  //
  0x43.bitRank(0); // returns 0   (found at index 0)
  0x43.bitRank(1); // returns 1   (found at index 1)
  0x43.bitRank(2); // returns 6   (found at index 6)
  0x43.bitRank(3); // returns -1  (not found)

Additional information #

These algorithms were inspired by Bit Twiddling Hacks by Sean Eron Anderson

1
likes
160
pub points
0%
popularity

Publisher

verified publisherbramp.net

Collection of bit twiddling hacks. Think _popcnt64 or _pdep_u64.

Repository (GitHub)
View/report issues

Topics

#bit #performance

Documentation

API reference

License

BSD-2-Clause (license)

More

Packages that depend on bithacks