A highly optimized and flexible library for working with bit vectors in C++. This library provides efficient operations for bit-level manipulations, including bitwise operations, arithmetic operations, and more.
- Features
- Installation
- Clone the repository
- Include in your project
- Usage
- Creating a BitVector
- Accessing and Modifying Bits
- Performing Operations
- Two's Complement
- Error Handling
- Contributing
- License
- Efficient bit operations: Perform bit-level manipulations like
AND
,OR
,XOR
, and shifts efficiently. - Arithmetic operations: Supports operations like addition, subtraction, multiplication, and division.
- Flexible bit sizes: Work with bit vectors of arbitrary sizes and perform operations on them seamlessly.
- In-place operations: Modify bit vectors directly with functions like
+=
,-=
, and*=
to minimize memory usage. - Two's complement: Easily compute the two's complement of bit vectors.
- Error handling: Includes error handling for common issues like division by zero or overflow during arithmetic operations.
To install the BitVector library, you can clone this repository or directly include the header files in your project.
git clone https://github.com/Galfurian/bitvector.git
Simply include the bitvector.hpp
file in your project:
#include "bvlib/bitvector.hpp"
The BitVector
class allows you to create bit vectors of arbitrary sizes and
perform a wide range of operations on them.
To create a BitVector
:
#include "bvlib/bitvector.hpp"
int main() {
// Create a 32-bit BitVector initialized with all bits set.
auto bv32 = bvlib::BitVector<32>::ones();
// Create a 64-bit BitVector initialized at zero.
auto bv64 = bvlib::BitVector<64>::zeros();
}
Use at()
to access a specific bit and set()
or reset()
to modify bits:
bv.set(0); // Set bit 0 to 1
bv.reset(1); // Set bit 1 to 0
bool bit = bv[0]; // Access bit 0 using operator[]
bv[1] = true; // Modify bit 1 using operator[]
You can perform arithmetic and bitwise operations directly on BitVector
objects.
bvlib::BitVector<8> bv1("11001100");
bvlib::BitVector<8> bv2("10101010");
auto result = bv1 & bv2; // Bitwise AND
result = bv1 | bv2; // Bitwise OR
result = bv1 ^ bv2; // Bitwise XOR
result = ~bv1; // Bitwise NOT
bvlib::BitVector<8> bv1("11001100");
bvlib::BitVector<8> bv2("10101010");
auto sum = bv1 + bv2; // Add two bit vectors
auto diff = bv1 - bv2; // Subtract two bit vectors
auto product = bv1 * bv2; // Multiply two bit vectors
auto quotient = bv1 / bv2; // Divide two bit vectors
bv1 += bv2; // Add and assign
bv1 -= bv2; // Subtract and assign
bv1 *= bv2; // Multiply and assign
You can compute the two's complement of a BitVector
:
bvlib::BitVector<8> bv("11110000");
bvlib::detail::two_complement(bv);
The library includes error handling for common scenarios, such as division by zero.
try {
auto quotient = bv1 / bv2; // May throw if bv2 is zero
} catch (const std::domain_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
We welcome contributions to the BitVector library! To contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-name
). - Make your changes.
- Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature-name
). - Open a pull request.
This project is licensed under the MIT License - see the LICENSE.md file for details.