Skip to content

Commit

Permalink
Docs: Add README
Browse files Browse the repository at this point in the history
  • Loading branch information
czs108 committed Nov 25, 2024
1 parent 0197308 commit f51b935
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cff-version: 1.2.0
authors:
- family-names: Chen
given-names: Zhenshuo
orcid: https://orcid.org/0000-0003-2091-4160
- family-names: Liu
given-names: Guowen
orcid: https://orcid.org/0000-0002-8375-5729
title: Bit Manipulation
date-released: 2024-07-20
url: https://github.com/Zhuagenborn/Bit-Manipulation
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Bit Manipulation

![C++](docs/badges/C++.svg)
[![CMake](docs/badges/Made-with-CMake.svg)](https://cmake.org)
![GitHub Actions](docs/badges/Made-with-GitHub-Actions.svg)
![License](docs/badges/License-MIT.svg)

## Introduction

A header-only bit manipulation library written in *C++20*, 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.

## Unit Tests

### Prerequisites

- Install *GoogleTest*.
- Install *CMake*.

### Building

Go to the project folder and run:

```bash
mkdir -p build
cd build
cmake ..
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<std::uint16_t, std::uint8_t>(0x12, 0x34)), 0x1234);
EXPECT_EQ((CombineBits<std::uint32_t, std::uint16_t>(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));
```

## License

Distributed under the *MIT License*. See `LICENSE` for more information.
1 change: 1 addition & 0 deletions docs/badges/C++.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/badges/License-MIT.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/badges/Made-with-CMake.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/badges/Made-with-GitHub-Actions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f51b935

Please sign in to comment.