https://github.com/bramp/bithacks
A collection of bit twiddling hacks in dart
https://github.com/bramp/bithacks
Last synced: 9 months ago
JSON representation
A collection of bit twiddling hacks in dart
- Host: GitHub
- URL: https://github.com/bramp/bithacks
- Owner: bramp
- License: bsd-2-clause
- Created: 2024-01-31T22:36:43.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-08T17:43:57.000Z (over 2 years ago)
- Last Synced: 2025-06-14T14:16:13.458Z (about 1 year ago)
- Language: Dart
- Homepage: https://pub.dev/packages/bithacks
- Size: 35.2 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# bithacks
by Andrew Brampton ([bramp.net](https://bramp.net))
Collection of bit twiddling hacks. Think
[popcount](https://en.cppreference.com/w/cpp/numeric/popcount) or _pdep_u64.
Works on both Dart VM and dart2js.
[GitHub](https://github.com/bramp/bithacks) | [Package](https://pub.dev/packages/bithacks) | [API Docs](https://pub.dev/documentation/bithacks/latest/)
## Usage
```dart
// 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)
```
## Development
To run the tests:
```bash
dart test
dart test -p chrome
```
To publish:
```bash
dart analyze
# Bump the version in pubspec.yaml
# Update the CHANGELOG.md
dart pub lish
```
## Additional information
These algorithms were inspired by [Bit Twiddling Hacks](https://graphics.stanford.edu/~seander/bithacks.html)
by Sean Eron Anderson