{"id":17977620,"url":"https://github.com/martinmoene/bit-lite","last_synced_at":"2025-03-25T15:31:44.598Z","repository":{"id":53745074,"uuid":"274167808","full_name":"martinmoene/bit-lite","owner":"martinmoene","description":"bit-lite - C++20 bit operations for C++98 and later in a single-file header-only library","archived":false,"fork":false,"pushed_at":"2024-01-14T15:24:13.000Z","size":80,"stargazers_count":40,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-20T13:24:33.219Z","etag":null,"topics":["bit","bit-implementation","bit-manipulation","cpp20","header-only","no-dependencies","single-file"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/martinmoene.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-22T15:00:07.000Z","updated_at":"2024-11-19T00:15:07.000Z","dependencies_parsed_at":"2023-01-23T05:00:53.206Z","dependency_job_id":null,"html_url":"https://github.com/martinmoene/bit-lite","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fbit-lite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fbit-lite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fbit-lite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fbit-lite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/martinmoene","download_url":"https://codeload.github.com/martinmoene/bit-lite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245489764,"owners_count":20623789,"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":["bit","bit-implementation","bit-manipulation","cpp20","header-only","no-dependencies","single-file"],"created_at":"2024-10-29T17:28:41.907Z","updated_at":"2025-03-25T15:31:42.752Z","avatar_url":"https://github.com/martinmoene.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bit-lite - C++20 bit operations for C++98 and later in a single-file header-only library.\n\n[![Language](https://img.shields.io/badge/C%2B%2B-98/11/14/17/20-blue.svg)](https://en.wikipedia.org/wiki/C%2B%2B#Standardization) [![License](https://img.shields.io/badge/license-BSL-blue.svg)](https://opensource.org/licenses/BSL-1.0) [![Build Status](https://github.com/martinmoene/bit-lite/actions/workflows/ci.yml/badge.svg)](https://github.com/martinmoene/bit-lite/actions/workflows/ci.yml) [![Build status](https://ci.appveyor.com/api/projects/status/gpmw4gt271itoy2n?svg=true)](https://ci.appveyor.com/project/martinmoene/bit-lite) [![Version](https://badge.fury.io/gh/martinmoene%2Fbit-lite.svg)](https://github.com/martinmoene/bit-lite/releases) [![download](https://img.shields.io/badge/latest-download-blue.svg)](https://raw.githubusercontent.com/martinmoene/bit-lite/master/include/nonstd/bit.hpp) [![Conan](https://img.shields.io/badge/on-conan-blue.svg)](https://conan.io/center/bit-lite) [![Try it online](https://img.shields.io/badge/on-wandbox-blue.svg)](https://wandbox.org/permlink/ZwnY4c9dZUAynUac) [![Try it on godbolt online](https://img.shields.io/badge/on-godbolt-blue.svg)](https://godbolt.org/z/1IPvn7)\n\n**Contents**  \n\n- [Example usage](#example-usage)\n- [In a nutshell](#in-a-nutshell)\n- [License](#license)\n- [Dependencies](#dependencies)\n- [Installation](#installation)\n- [Synopsis](#synopsis)\n- [Features](#features)\n- [Reported to work with](#reported-to-work-with)\n- [Building the tests](#building-the-tests)\n- [Other implementations of bit](#other-implementations-of-bit)\n- [Notes and references](#notes-and-references)\n- [Appendix](#appendix)\n\n## Example usage\n\n```Cpp\n#include \"nonstd/bit.hpp\"\n#include \u003ciostream\u003e\n\nusing namespace nonstd;\n\nint main()\n{\n    std::cout\n        \u003c\u003c \"Consecutive ones at the right in 0x17: \" \u003c\u003c countr_one( 0x17u )\n        \u003c\u003c \"\\nBit width of 0x13: \" \u003c\u003c bit_width( 0x13u ) \u003c\u003c '\\n';\n}\n```\n\n### Compile and run\n\n```Text\nprompt\u003e g++ -Wall -I../include -o 01-basic.exe 01-basic.cpp \u0026\u0026 01-basic.exe\nConsecutive ones at the right in 0x17: 3\nBit width of 0x13: 5\n```\n\n## In a nutshell\n\n**bit lite** is a single-file header-only library to provide the tools from the C++20 header [`\u003cbit\u003e`](https://en.cppreference.com/w/cpp/header/bit) for use with C++98 and later. If available, the standard header is used, unless [configured](#configuration) otherwise.\n\n**Features and properties of bit lite** are ease of installation (single header), freedom of dependencies other than the standard library.\n\n**Limitations of bit lite** are ... .\n\n## License\n\n*bit lite* is distributed under the [Boost Software License](https://github.com/martinmoene/bit-lite/blob/master/LICENSE.txt).\n\n## Dependencies\n\n*bit lite* has no other dependencies than the [C++ standard library](http://en.cppreference.com/w/cpp/header).\n\n## Installation\n\n*bit lite* is a single-file header-only library. Put `bit.hpp` in the [include](include) folder directly into the project source tree or somewhere reachable from your project.\n\n## Synopsis\n\n**Contents**  \n\n[Documentation of standard header `\u003cbit\u003e`](#documentation-of-stdbit)  \n[Non-standard extensions](#non-standard-extensions)  \n[Configuration](#configuration)  \n\n### Documentation of standard header `\u003cbit\u003e`\n\nDepending on the compiler and C++-standard used, *bit lite* behaves less or more like C++20 standard `\u003cbit\u003e`. To get an idea of the capabilities of *bit lite* with your configuration, look at the output of the [tests](test/bit.t.cpp), issuing `bit-main.t --pass @`. For C++20 standard `\u003cbit\u003e`, see its [documentation at cppreference](https://en.cppreference.com/w/cpp/header/bit).  \n\n### Non-standard extensions\n\n| Kind               | Extension type or function | Notes |\n|--------------------|----------------------------|-------|\n| **Type**           | **big_endian_type**        | type based on endian::big    |\n| \u0026nbsp;             | **little_endian_type**     | type based on endian::little |\n| \u0026nbsp;             | **native_endian_type**     | type based on endian::native |\n| \u0026nbsp;             | \u0026nbsp; |\u0026nbsp;|\n| **Free function**  | template\u0026lt;class T\u003e\u003cbr\u003eT **to_big_endian**(T v) | \u003cbr\u003econvert unconditionally |\n| \u0026nbsp;             | template\u0026lt;class T, class EN\u003e\u003cbr\u003eT **to_big_endian**(T v, EN) | convert if EN is little_endian_type\u003cbr\u003e*EN specifies endianness of v* |\n| \u0026nbsp;             | template\u0026lt;class T\u003e\u003cbr\u003eT **to_little_endian**(T v) | \u003cbr\u003econvert unconditionally |\n| \u0026nbsp;             | template\u0026lt;class T, class EN\u003e\u003cbr\u003eT **to_little_endian**(T v, EN) | convert if EN is big_endian_type\u003cbr\u003e*EN specifies endianness of v* |\n| \u0026nbsp;             | template\u0026lt;class T\u003e\u003cbr\u003eT **to_native_endian**(T v) | \u003cbr\u003eidentity |\n| \u0026nbsp;             | template\u0026lt;class T, class EN\u003e\u003cbr\u003eT **to_native_endian**(T v, EN) | convert if EN represents\u003cbr\u003e non-native endianness for v |\n| \u0026nbsp;             | \u0026nbsp; |\u0026nbsp;|\n| \u0026nbsp;             | template\u0026lt;class T\u003e\u003cbr\u003eT **as_big_endian**(T v) | \u003cbr\u003econvert if native is little endian |\n| \u0026nbsp;             | template\u0026lt;class T\u003e\u003cbr\u003eT **as_little_endian**(T v) | \u003cbr\u003econvert if native is big endian |\n| \u0026nbsp;             | template\u0026lt;class T\u003e\u003cbr\u003eT **as_native_endian**(T v) | \u003cbr\u003eidentity |\n\n### Configuration\n\n#### Standard selection macro\n\n\\-D\u003cb\u003ebit\\_CPLUSPLUS\u003c/b\u003e=199711L  \nDefine this macro to override the auto-detection of the supported C++ standard, if your compiler does not set the `__cplusplus` macro correctly.\n\n#### Select C++20 standard `\u003cbit\u003e` or nonstd `\u003cbit\u003e`\n\nAt default, *bit lite* uses the C++20 standard header `\u003cbit\u003e` if it is available and lets you use it via namespace `nonstd`. You can however override this default and explicitly request to use the standard C++20 header or *bit lite*'s header via the following macros.\n\n-D\u003cb\u003ebit\\_CONFIG\\_SELECT\\_BIT\u003c/b\u003e=bit_BIT_DEFAULT  \nDefine this to `bit_BIT_STD` to select C++20 standard header `\u003cbit\u003e`. Define this to `bit_BIT_NONSTD` to select *bit lite*. Default is undefined, which has the same effect as defining to `bit_BIT_DEFAULT`.\n\n#### Strict C++20 mode\n\n-D\u003cb\u003ebit\\_CONFIG\\_STRICT\u003c/b\u003e=1  \nDefine this to `1` to omit the non C++20 standard extensions. Default is undefined, which has the same effect as `0`, non-strict mode.\n\n## Reported to work with\n\nTBD\n\n## Building the tests\n\nTBD\n\n## Other implementations of `\u003cbit\u003e`\n\nTBD\n\n## Notes and references\n\n*Interface and specification*\n\n- Cppreference. [Header \u0026lt;bit\u0026gt;](https://en.cppreference.com/w/cpp/header/bit)\n- Cppreference. [bit_cast](https://en.cppreference.com/w/cpp/numeric/bit_cast)\n- Cppreference. [enum class endian](https://en.cppreference.com/w/cpp/types/endian)\n\n*Proposals*\n\n- [p0463 - endendian, Just endian](https://wg21.link/P0463). Howard E. Hinnant. 2017.\n- [p0476 - Bit-casting object representations](https://wg21.link/P0476). JF Bastien. 2017.\n- [p0553 - Bit operations](https://wg21.link/P0553). Jens Maurer. 2019.\n\n*Articles*\n\n- Overload 160. [What is the Strict Aliasing Rule and Why Do We Care?](https://accu.org/journals/overload/28/160/anonymous/). Anonymous. December 2020.\n\n## Appendix\n\nThe [test program](test/bit.t.cpp) provides information on the compiler, the C++ language and library capabilities and the tests performed.\n\n### A.1 Compile-time information\n\nThe version of *bit lite* is available via tag `[.version]`. The following tags are available for information on the compiler and on the C++ standard library used: `[.compiler]`, `[.stdc++]`, `[.stdlanguage]` and `[.stdlibrary]`.\n\n### A.2 Bit lite test specification\n\n```Text\nbit_cast\u003c\u003e(): successfully roundtrips uint64_t via double [bit.cast]\nhas_single_bit(): single bit yields false for no bits set [bit.pow.two]\nhas_single_bit(): single bit yields true for single bits set [bit.pow.two]\nhas_single_bit(): single bit yields false for multiple bits set [bit.pow.two]\nbit_ceil(): let N be the smallest power of 2 greater than or equal to x [bit.pow.two]\nbit_floor(): x == 0, 0; otherwise the maximal value y such that has_single_bit(y) is true and y \u003c= x [bit.pow.two]\nbit_width: x == 0, 0; otherwise one plus the base-2 logarithm of x, with any fractional part discarded [bit.pow.two]\nrotl(): r is 0, x; if r is positive, (x \u003c\u003c r) | (x \u003e\u003e (N - r)); if r is negative, rotr(x, -r) [bit.rotate]\nrotr(): r is 0, x; if r is positive, (x \u003e\u003e r) | (x \u003c\u003c (N - r)); if r is negative, rotl(x, -r) [bit.rotate]\ncountl_zero(): the number of consecutive 0 bits in the value of x, starting from the most significant bit [bit.count]\ncountl_one(): the number of consecutive 1 bits in the value of x, starting from the most significant bit [bit.count]\ncountr_zero(): the number of consecutive 0 bits in the value of x, starting from the least significant bit [bit.count]\ncountr_one(): the number of consecutive 1 bits in the value of x, starting from the least significant bit [bit.count]\npopcount(): the number of 1 bits in the value of x [bit.count]\nendian: little differs from big (corner-case when all scalars have size of 1 byte) [bit.endian]\nto_big_endian(): convert native, or little or big endian specified unsigned to big endian [bit.endian.extension]\nto_little_endian(): convert native, or little or big endian specified unsigned to little endian [bit.endian.extension]\nto_native_endian(): convert native, or little or big endian specified unsigned to native endian [bit.endian.extension]\nas_big_endian(): provide native unsigned as big endian [bit.endian.extension]\nas_little_endian(): provide native unsigned as little endian [bit.endian.extension]\nas_native_endian(): provide native unsigned as native endian (identity) [bit.endian.extension]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartinmoene%2Fbit-lite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartinmoene%2Fbit-lite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartinmoene%2Fbit-lite/lists"}