https://github.com/zhuagenborn/bit-manipulation
🛠️ A C++23 header-only bit manipulation library supporting getting, setting, clearing, filling and combining bits.(使用C++23开发的比特位操作库,支持获取、设置、清除、填充和组合操作。)
https://github.com/zhuagenborn/bit-manipulation
bit-field bit-manipulation cpp23
Last synced: 4 months ago
JSON representation
🛠️ A C++23 header-only bit manipulation library supporting getting, setting, clearing, filling and combining bits.(使用C++23开发的比特位操作库,支持获取、设置、清除、填充和组合操作。)
- Host: GitHub
- URL: https://github.com/zhuagenborn/bit-manipulation
- Owner: Zhuagenborn
- License: mit
- Created: 2024-07-21T15:23:30.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-09-18T03:00:25.000Z (9 months ago)
- Last Synced: 2025-09-18T05:21:50.088Z (9 months ago)
- Topics: bit-field, bit-manipulation, cpp23
- Language: C++
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
README
# Bit Manipulation

[](https://cmake.org)


## Introduction
A header-only bit manipulation library written in *C++23*, supporting:
- Getting bits, bytes, words or double words in an integral value.
- Clearing bits, bytes, words or double words in an integral value.
- Setting bits, bytes, words or double words in an integral value.
- Filling bits, bytes, words or double words in an integral value.
- Combining bits, bytes, words or double words to a larger integral value.
- Writing or reading bytes of an integral or a float value using the specified endianness.
## Unit Tests
### Prerequisites
- Install *GoogleTest*.
- Install *CMake*.
### Building
Go to the project folder and run:
```bash
mkdir -p build
cd build
cmake -DBIT_MANIP_BUILD_TESTS=ON ..
cmake --build .
```
### Running
Go to the `build` folder and run:
```bash
ctest -VV
```
## Examples
See more examples in `tests/bit_manip_tests.cpp`.
```c++
// Get the specified bits in an integral value.
constexpr std::uint32_t val {0x12345678};
EXPECT_EQ(GetBits(val, 0, sizeof(std::uint8_t) * CHAR_BIT), 0x78);
EXPECT_EQ(GetBits(val, 0, sizeof(std::uint16_t) * CHAR_BIT), 0x5678);
```
```c++
// Set the value of the specified bits in an integral value.
std::uint32_t val {0x12345678};
SetBits(val, 0xFFFF, 0, sizeof(std::uint16_t) * CHAR_BIT);
EXPECT_EQ(val, 0x1234FFFF);
```
```c++
// Fill the specified bits in an integral value.
std::uint32_t val {0x12345678};
FillBits(val, 0, sizeof(std::uint8_t) * CHAR_BIT);
EXPECT_EQ(val, 0x123456FF);
```
```c++
// Clear the specified bits in an integral value.
std::uint32_t val {0x12345678};
ClearBits(val, 0, sizeof(std::uint16_t) * CHAR_BIT);
EXPECT_EQ(val, 0x12340000);
```
```c++
// Combine low bits and high bits into a new integral value.
EXPECT_EQ((CombineBits(0x12, 0x34)), 0x1234);
EXPECT_EQ((CombineBits(0x1234, 0x5678)), 0x12345678);
```
```c++
// Check if a bit is set in an integral value.
constexpr std::uint8_t val {0b0000'0001};
EXPECT_TRUE(IsBitSet(val, 0));
EXPECT_FALSE(IsBitSet(val, 1));
```
```c++
// Read bytes using the specified endianness.
constexpr int buf {0x12345678};
int val {0};
ReadBytes(std::as_bytes(std::span {&buf, 1}), val, GetOppositeEndian());
EXPECT_EQ(val, std::byteswap(buf));
```
## License
Distributed under the *MIT License*. See `LICENSE` for more information.