{"id":13894191,"url":"https://github.com/RobTillaart/bitHelpers","last_synced_at":"2025-07-17T09:31:07.467Z","repository":{"id":42049851,"uuid":"283244627","full_name":"RobTillaart/bitHelpers","owner":"RobTillaart","description":"Arduino library with static functions on bit level (a.k.a. bit hacks)","archived":false,"fork":false,"pushed_at":"2024-04-13T09:15:39.000Z","size":50,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-14T18:53:35.894Z","etag":null,"topics":["arduino","bit"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RobTillaart.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"RobTillaart","custom":"https://www.paypal.me/robtillaart"}},"created_at":"2020-07-28T14:56:31.000Z","updated_at":"2024-01-25T19:40:18.000Z","dependencies_parsed_at":"2024-04-13T10:29:27.200Z","dependency_job_id":null,"html_url":"https://github.com/RobTillaart/bitHelpers","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/RobTillaart/bitHelpers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FbitHelpers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FbitHelpers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FbitHelpers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FbitHelpers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RobTillaart","download_url":"https://codeload.github.com/RobTillaart/bitHelpers/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FbitHelpers/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265589083,"owners_count":23793465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino","bit"],"created_at":"2024-08-06T18:01:26.149Z","updated_at":"2025-07-17T09:31:07.149Z","avatar_url":"https://github.com/RobTillaart.png","language":"C++","funding_links":["https://github.com/sponsors/RobTillaart","https://www.paypal.me/robtillaart"],"categories":["C++"],"sub_categories":[],"readme":"\n[![Arduino CI](https://github.com/RobTillaart/BitHelpers/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)\n[![Arduino-lint](https://github.com/RobTillaart/BitHelpers/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/BitHelpers/actions/workflows/arduino-lint.yml)\n[![JSON check](https://github.com/RobTillaart/BitHelpers/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/BitHelpers/actions/workflows/jsoncheck.yml)\n[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/BitHelpers.svg)](https://github.com/RobTillaart/BitHelpers/issues)\n\n[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/BitHelpers/blob/master/LICENSE)\n[![GitHub release](https://img.shields.io/github/release/RobTillaart/BitHelpers.svg?maxAge=3600)](https://github.com/RobTillaart/BitHelpers/releases)\n[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/BitHelpers.svg)](https://registry.platformio.org/libraries/robtillaart/BitHelpers)\n\n\n# bitHelpers\n\nArduino library with functions on bit level.\n\n\n## Description\n\nThis library contains functions to manipulate bits and bit patterns in an \nefficient way. \nFor most functions a 8 - 64 bit optimized version exist. \n\nThe library is tested on ESP32 and Arduino UNO but not for all possible values. \nother platforms are expected to work without modification. \nIf they don't please file an issue on GitHub.\n\nNew bit functions can be added or investigated, please file an issue on GitHub.\n\n\n#### Related\n\n- https://github.com/RobTillaart/BitArray\n\n\n## Interface\n\n```cpp\n#include \"bitHelpers.h\"\n```\n\n#### BitCount\n\nseveral implementations to compare performance.\n\n- **uint8_t bitCountReference(uint32_t value)** returns number of bits set in a value.\n- **uint8_t bitCountKR(uint32_t value)** Kerningham Ritchie bitCount.\n- **uint8_t bitCountArray(uint32_t value)** count per nibble with lookup table.\n- **uint8_t bitCountF1(uint32_t value)** SWAG algorithm variant.\n- **uint8_t bitCountF2(uint32_t value)** SWAG algorithm variant.\n\nBitCount - fastest version, SWAG algorithm\n\n- **uint8_t  bitCount(uint8_t value)**\n- **uint8_t  bitCount(uint16_t value)**\n- **uint8_t  bitCount(uint32_t value)**\n- **uint8_t  bitCount(uint64_t value)**\n\n\n#### Reverse\n\nT = uint8_t .. uint64_t\n\n- **T bitReverse(T value)** reverses bits.\n- **T nybbleReverse(T value)** reverses nibbles (4 bit) in a uint8_t .. uint64_t.\n- **T byteReverse(T value)** reverses bytes (8 bit) in a uint16_t .. uint64_t.\n- **T wordReverse(T value)** reverses words (16 bit) in uint32_t and uint64_t.\n\n#### Swap \n\nswap upper and lower half: uint8_t .. uint64_t. Is like rotate 50%\n\n- **T swap(T value)** 0x12345678 ==\u003e 0x56781234.\n\n\n#### BitRotate\n\nRotate Left / Right: uint8_t .. uint64_t\nif position larger than # bits original value is returned.\n\n- **T bitRotateLeft(T value, uint8_t position)**\n- **T bitRotateRight(T value, uint8_t position)** \n\n\n#### BitFlip\n\nBitFlip: uint8_t .. uint64_t  a.k.a toggle\nif position larger than # bits original value is returned.\n\n- **T bitFlip(T value, uint8_t position)** flips a single bit at position\n\n\n#### BitRot\n\nBitRot: uint8_t .. uint64_t\n\n- **T bitRotRef(T value, float chance = 0.5, uint8_t times = 1)** reference implementation.\n- **T bitRot(T value, float chance = 0.5, uint8_t times = 1)** random damage to a single bit of a value,\nchance = float 0.0 .. 1.0 that one random bit is toggled. \nThe times parameter allows to apply this n times.\n**bitRot()** is a function that can be used to mimic (single) bit errors in communication protocols.  \n*Note: a chance of 50% for 2 uint8_t is not equal to 50% chance for 1 uint16_t.*\n\n\n#### BitsNeeded\n\nHow many bits are needed to store / transmit a number?\n\n- **bitsNeededReference(n)** reference implementation for uint8_t to uint64_t.\n- **bitsNeeded(n)** A 'recursive strategy' for uint8_t .. uint64_t provides a fast answer. \n\n\n#### BitSet64 et al.\n\nThe following functions are made as the normal **bitset()** etcetera do not work for 64 bit.\nThese functions are optimized for speed for **AVR**, **ESP32** and **ESP8266**. \n\n- **void bitSet64(uint64_t \u0026 x, uint8_t bit)** set bit of uint64_t\n- **void bitClear64(uint64_t \u0026 x, uint8_t bit)** clear bit of uint64_t\n- **void bitToggle64(uint64_t \u0026 x, uint8_t bit)** toggle bit of uint64_t\n- **void bitWrite64(uint64_t \u0026 x, uint8_t bit, uint8_t value)** set bit of uint64_t to 0 or 1\n- **void bitRead64(uint64_t \u0026 x, uint8_t bit)** reads bit from uint64_t \n\nAlso added are macro versions of these five functions.\n\n- **mbitSet64(x, bit)** set bit of uint64_t\n- **mbitClear64(x, bit)** clear bit of uint64_t\n- **mbitToggle64(x, bit)** toggle bit of uint64_t\n- **mbitWrite64(x, bit, value)** set bit of uint64_t to 0 or 1\n- **mbitRead64(x, bit)** reads bit from uint64_t \n\n\n## Future\n\n#### Must\n\n- improve documentation\n- improve readability of code\n\n#### Should\n\n- add performance tests\n- **bitRotateLeftRight()** should it do modulo position?\n- **bitsNeededRef()** correct for value 0?\n- **nybbleReverse()** =\u003e **nibbleReverse()**\n\n\n#### Could\n\n- besides **bitRot()** one can also have timing issues when clocking in bits. \nA function could be created to mimic such timing error, by shifting bits from a \nspecific position. e.g. \n- **parShiftLeft(00001010, 3)** ==\u003e 00011010\n- **bitBurst(00000000, 3)** ==\u003e  00111000 any group of 3 bits will toggle. edges?\n- **bitRot(value, chance = 50%, times = 1)** extension...\n- **bitNoggle(value, bit)** - toggle all but one bit. (why?)\n- **bitSort(value)** 00101001 ==\u003e 00000111\nor with minimal # toggles?\n- **bitReverse(uint32_t x, uint8_t n)** see below.\n- **byteReverse24(uint32_t x)** dedicated 24 bit = 3 bytes e.g RGB ==\u003e BGR\n- **byteRotate24(uint32_t x)** dedicated 24 bit = 3 bytes e.g RGB\n- **byteInverse(uint32_t x)** (a,b,c,d) =\u003e (255-a, 255-b, 255-c, 255-d) = rather simple ~?\n- **isBitPalindrome()** byte, word ...\n- **bitSwap(value, p, q)** \n- many more :)\n\n\n#### Wont\n\n\n## ideas\n\n#### BitReverse n bit number\n\nTrick to reverse a number of n bits  ( 0 \u003c n \u003c 32 ).\nCould also be done similar with 64 bit and or byte / nibble reverse.\n\nnot as fast as a dedicated version.\n```cpp\nuint32_t bitReverse(uint32_t x, uint8_t n)\n{\n  uint32_t r = bitReverse(x);\n  return r \u003e\u003e (32 - n);       // reverse only top n bits.\n}\n```\nCould be added in next release...\n\nQ: what to do with the first (32-n) bits?\nJust reverse the last 24 bits and clear bit 24-31 is different than\nreversing the last 24 bits and keep bit 24-31 as is.\n```cpp\nuint32_t bitReverse(uint32_t x, uint8_t n)\n{\n  uint32_t y = (x \u003e\u003e n) \u003c\u003c n;\n  uint32_t r = bitReverse(x);\n  r \u003e\u003e= (32 - n);\n  return y | r;\n}\n```\n\n\n## Support\n\nIf you appreciate my libraries, you can support the development and maintenance.\nImprove the quality of the libraries by providing issues and Pull Requests, or\ndonate through PayPal or GitHub sponsors.\n\nThank you,\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRobTillaart%2FbitHelpers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRobTillaart%2FbitHelpers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRobTillaart%2FbitHelpers/lists"}