{"id":40779915,"url":"https://github.com/nessan/bit","last_synced_at":"2026-01-21T19:17:12.601Z","repository":{"id":239598977,"uuid":"799989069","full_name":"nessan/bit","owner":"nessan","description":"C++ header-only library for working in bit-space/GF(2).","archived":false,"fork":false,"pushed_at":"2024-08-28T17:21:55.000Z","size":2045,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-08-28T20:29:18.450Z","etag":null,"topics":["bitmatrix","bitset","bitvector","gf2","linear-algebra","polynomial"],"latest_commit_sha":null,"homepage":"https://nessan.github.io/bit","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/nessan.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":null,"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}},"created_at":"2024-05-13T13:45:14.000Z","updated_at":"2024-08-28T17:21:59.000Z","dependencies_parsed_at":"2024-08-25T19:54:17.117Z","dependency_job_id":"b7a09fc5-08d9-4338-87b2-6661450becf2","html_url":"https://github.com/nessan/bit","commit_stats":null,"previous_names":["nessan/bit"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/nessan/bit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nessan%2Fbit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nessan%2Fbit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nessan%2Fbit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nessan%2Fbit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nessan","download_url":"https://codeload.github.com/nessan/bit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nessan%2Fbit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28640777,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-21T18:04:35.752Z","status":"ssl_error","status_checked_at":"2026-01-21T18:03:55.054Z","response_time":86,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["bitmatrix","bitset","bitvector","gf2","linear-algebra","polynomial"],"created_at":"2026-01-21T19:17:12.525Z","updated_at":"2026-01-21T19:17:12.588Z","avatar_url":"https://github.com/nessan.png","language":"C++","readme":"# README\n\n`bit` is a header-only C++ library for numerical work in _bit-space_, which mathematicians call [GF2][]. This is the simplest Galois field with just two elements, 0 and 1. All arithmetic operations in bit-space are mod 2, so what starts in bit-space stays in bit-space.\n\nThe library provides vector and matrix classes for performing linear algebra in bit-space. The [`bit::vector`][] class represents _bit_vectors_, and the [`bit::matrix`][] class represents _bit-matrices_. The library also has a [`bit::polynomial`][] class to represent _bit-polynomials_ over GF(2).\n\nThese classes are efficient and pack the individual bit elements into natural word blocks. You can size/resize the classes at run-time.\n\nBecause arithmetic operations in GF(2) are mod 2, addition/subtraction becomes the `XOR` operation, and multiplication/division becomes the `AND` operation. The `bit` library uses those equivalences to efficiently perform most interactions on and between bit-vectors and bit-matrices by simultaneously working on whole blocks of elements.\n\nThe `bit` library provides a rich interface to set up and manipulate bit-vectors and bit-matrices in various ways. Amongst other things, the interface includes methods to solve systems of linear equations over GF(2) and to look at the eigen-structure of bit-matrices.\n\nThe `bit::polynomial` class has methods to compute $x^N\\bmod{p(x)}$ where $p(x)$ is a polynomial over $\\mathbb{F}_2$ and $N$ is a potentially huge integer.\n\n## Example\n\nHere is a simple example of a program that uses `bit`:\n\n```cpp\n#include \u003cbit/bit.h\u003e\nint main()\n{\n    auto M = bit::matrix\u003c\u003e::random(6, 6);\n    auto c = bit::characteristic_polynomial(M);\n    std::cout \u003c\u003c \"The bit-matrix M:\\n\" \u003c\u003c M \u003c\u003c \"\\n\";\n    std::cout \u003c\u003c \"has characteristic polynomial c(x) = \" \u003c\u003c c \u003c\u003c \".\\n\";\n    std::cout \u003c\u003c \"The polynomial sum c(M) gives:\\n\";\n    std::cout \u003c\u003c c(M) \u003c\u003c \"\\n\";\n}\n```\n\nThis program creates a random 6 x 6 bit-matrix `M` where 0 \u0026 1 are equally likely to occur and then extracts its characteristic polynomial $c(x) = c_0 + c_1 x + c_2 x^2 + ... + c_6 x^6$. Finally, the program verifies that `M` satisfies its characteristic equation as expected from the Cayley-Hamilton theorem.\n\nHere is the output from one run of the program:\n\n```sh\nThe bit-matrix M:\n│0 1 1 0 0 0│\n│0 0 1 0 1 0│\n│1 1 0 0 0 1│\n│0 0 0 0 0 1│\n│0 1 0 0 1 1│\n│1 1 0 1 0 1│\nhas characteristic polynomial c(x) = x^1 + x^4 + x^6.\nThe polynomial sum c(M) gives:\n│0 0 0 0 0 0│\n│0 0 0 0 0 0│\n│0 0 0 0 0 0│\n│0 0 0 0 0 0│\n│0 0 0 0 0 0│\n│0 0 0 0 0 0│\n```\n\n**NOTE:** `bit` makes it possible to quickly extract the characteristic polynomial for a bit-matrix with millions of elements. This problem chokes a naive implementation that needs to consider the unique nature of arithmetic in GF(2).\n\n## Installation\n\nThis library is header-only, so there is nothing to compile and link—drop the `bit` include directory somewhere convenient, and you're good to go.\n\nAlternatively, if you are using `CMake`, you can use the standard `FetchContent` module by adding a few lines to your project's `CMakeLists.txt` file:\n\n```cmake\ninclude(FetchContent)\nFetchContent_Declare(bit URL https://github.com/nessan/bit/releases/download/current/bit.zip)\nFetchContent_MakeAvailable(bit)\n```\n\nThis command downloads and unpacks an archive of the current version of the `bit` library to your project's build folder. You can then add a dependency on `bit::bit`, a `CMake` alias for `bit`. `FetchContent` will automatically ensure the build system knows where to find the downloaded header files and any needed compiler flags.\n\nUsed like this, `FetchContent` will only download a minimal library version without any redundant test code, sample programs, documentation files, etc.\n\n## Why Use `bit`?\n\nThe standard library already has [`std::bitset`][], an efficient _bitset_ class that is familiar and well thought through, so our `bit::vector` class replicates and extends much of that interface.\n\nAll `std::bitset` objects have a fixed size determined at compile time. The well-known _Boost_ library adds a dynamic version [`boost::dynamic_bitset`][], where the bitset size can be set and changed at runtime.\n\nHowever, as the two names suggest, those types are aimed at _bitsets_ instead of _bit-vectors_. So, for example, they print in _bit-order_ with the least significant element/bit on the right. More importantly, those classes don't have any particular methods aimed at linear algebra, and neither does the standard library's vector class `std::vector`.\n\nOn the other hand, several well-known linear algebra libraries, such as [Eigen][], exist. Those packages efficiently manage all the standard _numeric_ types (floats, doubles, integers, etc.) but do not correctly handle GF(2). You can create matrices of integers where all the elements are 0 or 1, but there is no built-in knowledge in those libraries that arithmetic is mod 2.\n\nFor example, you might use `Eigen` to create an integer matrix of all 0's and 1's and then use a built-in function from that library to extract the characteristic polynomial. Modding the coefficients of that polynomial with 2 gets the appropriate version for GF(2). Technically, this works, but you will have overflow problems for even relatively modest-sized matrices with just a few hundred rows and columns. Of course, you might use an underlying `BitInt` type that never overflows, but the calculations become dog slow for larger bit-matrices, which doesn't help much.\n\nThis specialised `bit` library is better for linear algebra problems over GF(2). Consider it if, for example, your interest is in cryptography or random number generation.\n\n## Documentation\n\nYou can read the project's documentation [here](https://nessan.github.io/bit/).\nThe documentation site was generated using [Quarto](https://quarto.org).\n\n### Contact\n\nYou can contact me by email [here](mailto:nzznfitz+gh@icloud.com).\n\n### Copyright and License\n\nCopyright (c) 2022-present Nessan Fitzmaurice.\nYou can use this software under the [MIT license](https://opensource.org/license/mit).\n\n\u003c!-- Reference Links --\u003e\n\n[GF2]: https://en.wikipedia.org/wiki/Finite_field\n[Eigen]: https://eigen.tuxfamily.org/overview.php?title=Main_Page\n[`bit::vector`]: https://nessan.github.io/bit/pages/vector/\n[`bit::matrix`]: https://nessan.github.io/bit/pages/matrix/\n[`bit::polynomial`]: https://nessan.github.io/bit/pages/polynomial/\n[`std::bitset`]: https://en.cppreference.com/w/cpp/utility/bitset\n[`boost::dynamic_bitset`]: https://www.boost.org/doc/libs/1_80_0/libs/dynamic_bitset/dynamic_bitset.html\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnessan%2Fbit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnessan%2Fbit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnessan%2Fbit/lists"}