https://github.com/mariosieg/bit-twiddling-hacks-collection
A single C header with many bitwise hacks
https://github.com/mariosieg/bit-twiddling-hacks-collection
Last synced: 8 months ago
JSON representation
A single C header with many bitwise hacks
- Host: GitHub
- URL: https://github.com/mariosieg/bit-twiddling-hacks-collection
- Owner: MarioSieg
- License: unlicense
- Created: 2020-07-21T23:07:27.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-11-27T10:58:32.000Z (over 4 years ago)
- Last Synced: 2025-04-03T18:51:24.640Z (about 1 year ago)
- Language: C
- Homepage:
- Size: 283 KB
- Stars: 30
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
A single C header files containing functions with different bit twiddling hacks and a short explanation.
I've put them together for myself as a cheatsheet and to play around with them.
If you know any hacks not included, just submit a PR, or if you encounter any wrong stuff or a typo!
Short hack assortment:
* Number multiplication, division
* Even, odd and power checks
* Uppercase, lowercase
* Set, clear and flip bits
* Insert and extract bitfields into words
* Convert LSL, MSB, trailing bits
* Masked copy
* Bit swap
* Population count
* Bit island count
* Bit scans
* Lexiocographic permutations
* Swapping without temporaries
* Size comparison: min() and max() functions without branching
* Modular additions
* Power rounding
* Significant masking
* Logbase2 of power2 calculation using deBruijin sequence
* Fast inverse sqrt
* Bit reversion
* Masked color operations
* XOR en/decryption
* Branchless assignments