Library for bit manipulation.
Loading...
Searching...
No Matches
bits

Library for bit manipulation.

Release License

Examples of usage

xmake

add_rules("mode.debug", "mode.release")
add_repositories("ttldtor https://github.com/ttldtor/xmake-repo.git")
add_requires("bits v1.0.0")
target("test_bits")
set_kind("binary")
add_packages("bits")
add_files("src/*.cpp")
set_languages("c++20")

CMake

cmake_minimum_required(VERSION 3.16)
project(test_bits LANGUAGES CXX)
include(FetchContent)
FetchContent_Declare(
bits
GIT_REPOSITORY https://github.com/ttldtor/bits.git
GIT_TAG v1.0.0
)
FetchContent_MakeAvailable(bits)
add_executable(${PROJECT_NAME} src/main.cpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
target_link_libraries(${PROJECT_NAME} PRIVATE bits)

Code

#include <iostream>
#include <bits/bits.hpp>
using namespace org::ttldtor::bits;
int main(int argc, char** argv) {
std::cout << "hello world!" << std::endl;
std::cout << shl(-5, 4) << std::endl;
return 0;
}
Library for bit manipulation.
constexpr ValueType shl(ValueType value, ShiftType shift) noexcept
Performs a left logical bit shift operation (shl, <<<).
Definition bits.hpp:318

Benchmarks

A possibly complete, but probably not very useful example of using this library.

Color Manipulation Example


// Copyright (c) 2025 ttldtor.
// SPDX-License-Identifier: BSL-1.0
#include <bits/bits.hpp>
#include <cstdint>
#include <iomanip>
#include <iostream>
using namespace org::ttldtor::bits;
struct Color {
std::uint8_t red;
std::uint8_t green;
std::uint8_t blue;
std::uint8_t alpha;
[[nodiscard]] std::uint32_t pack() const {
std::uint32_t result = 0;
// Shift alpha to bits 24-31 and OR with the result
result = orOp(result, shl(static_cast<std::uint32_t>(alpha), 24));
// Shift red to bits 16-23 and OR with the result
result = orOp(result, shl(static_cast<std::uint32_t>(red), 16));
// Shift green to bits 8-15 and OR with the result
result = orOp(result, shl(static_cast<std::uint32_t>(green), 8));
// Blue occupies bits 0-7, no shift needed
result = orOp(result, static_cast<std::uint32_t>(blue));
return result;
}
[[nodiscard]] static Color unpack(std::uint32_t packed) {
Color color{};
// Extract alpha from bits 24-31
color.alpha = static_cast<std::uint8_t>(shr(packed, 24));
// Extract red from bits 16-23
color.red = static_cast<std::uint8_t>(shr(packed, 16));
// Extract green from bits 8-15
color.green = static_cast<std::uint8_t>(shr(packed, 8));
// Extract blue from bits 0-7 (no shift needed, just mask)
color.blue = static_cast<std::uint8_t>(packed);
return color;
}
[[nodiscard]] static std::uint32_t setAlpha(std::uint32_t packed, std::uint8_t alpha) {
// Clear the alpha channel (bits 24-31)
packed = resetBits(packed, 0xFF000000u);
// Set a new alpha value
return setBits(packed, shl(static_cast<std::uint32_t>(alpha), 24));
}
[[nodiscard]] static std::uint32_t setRed(std::uint32_t packed, std::uint8_t red) {
// Clear the red channel (bits 16-23)
packed = resetBits(packed, 0x00FF0000u);
// Set a new red value
return setBits(packed, shl(static_cast<std::uint32_t>(red), 16));
}
[[nodiscard]] static std::uint32_t setGreen(std::uint32_t packed, std::uint8_t green) {
// Clear the green channel (bits 8-15)
packed = resetBits(packed, 0x0000FF00u);
// Set a new green value
return setBits(packed, shl(static_cast<std::uint32_t>(green), 8));
}
[[nodiscard]] static std::uint32_t setBlue(std::uint32_t packed, std::uint8_t blue) {
// Clear the blue channel (bits 0-7)
packed = resetBits(packed, 0x000000FFu);
// Set a new blue value
return setBits(packed, static_cast<std::uint32_t>(blue));
}
[[nodiscard]] static std::uint32_t argbToRgba(std::uint32_t argb) {
// Extract individual channels
const auto alpha = static_cast<std::uint8_t>(shr(argb, 24));
const auto red = static_cast<std::uint8_t>(shr(argb, 16));
const auto green = static_cast<std::uint8_t>(shr(argb, 8));
const auto blue = static_cast<std::uint8_t>(argb);
// Repack in RGBA format
std::uint32_t rgba = 0;
rgba = orOp(rgba, shl(static_cast<std::uint32_t>(red), 24));
rgba = orOp(rgba, shl(static_cast<std::uint32_t>(green), 16));
rgba = orOp(rgba, shl(static_cast<std::uint32_t>(blue), 8));
rgba = orOp(rgba, static_cast<std::uint32_t>(alpha));
return rgba;
}
[[nodiscard]] static Color blend(const Color& foreground, const Color& background) {
// Convert alpha from 0-255 to 0.0-1.0 range
const float alpha = static_cast<float>(foreground.alpha) / 255.0f;
const float invAlpha = 1.0f - alpha;
Color result{};
result.red = static_cast<std::uint8_t>(static_cast<float>(foreground.red) * alpha +
static_cast<float>(background.red) * invAlpha);
result.green = static_cast<std::uint8_t>(static_cast<float>(foreground.green) * alpha +
static_cast<float>(background.green) * invAlpha);
result.blue = static_cast<std::uint8_t>(static_cast<float>(foreground.blue) * alpha +
static_cast<float>(background.blue) * invAlpha);
result.alpha = 255; // Result is fully opaque
return result;
}
[[nodiscard]] static bool hasTransparency(std::uint32_t packed) {
// Extract the alpha channel and check if it's less than 255
const auto alpha = static_cast<std::uint8_t>(shr(packed, 24));
return alpha < 255;
}
[[nodiscard]] static std::uint32_t invert(std::uint32_t packed) {
// XOR with 0x00FFFFFF to invert RGB channels, leaving alpha unchanged
return xorOp(packed, 0x00FFFFFFu);
}
void print() const {
std::cout << "RGBA(" << static_cast<int>(red) << ", " << static_cast<int>(green) << ", " << static_cast<int>(blue)
<< ", " << static_cast<int>(alpha) << ")";
}
};
int main() {
std::cout << "=== Color Manipulation Example ===" << std::endl;
std::cout << std::hex << std::uppercase << std::setfill('0');
// Example 1: Pack and unpack color
std::cout << "\n--- Example 1: Pack and Unpack ---" << std::endl;
Color color1{255, 128, 64, 200};
std::cout << "Original color: ";
color1.print();
std::cout << std::endl;
std::uint32_t packed = color1.pack();
std::cout << "Packed (ARGB): 0x" << std::setw(8) << packed << std::endl;
Color unpacked = Color::unpack(packed);
std::cout << "Unpacked color: ";
unpacked.print();
std::cout << std::endl;
// Example 2: Modify individual channels
std::cout << "\n--- Example 2: Modify Channels ---" << std::endl;
std::uint32_t color2 = Color{100, 150, 200, 255}.pack();
std::cout << "Original: 0x" << std::setw(8) << color2 << " -> ";
Color::unpack(color2).print();
std::cout << std::endl;
color2 = Color::setRed(color2, 255);
std::cout << "After setRed(255): 0x" << std::setw(8) << color2 << " -> ";
Color::unpack(color2).print();
std::cout << std::endl;
color2 = Color::setAlpha(color2, 128);
std::cout << "After setAlpha(128): 0x" << std::setw(8) << color2 << " -> ";
Color::unpack(color2).print();
std::cout << std::endl;
// Example 3: Format conversion
std::cout << "\n--- Example 3: ARGB to RGBA ---" << std::endl;
std::uint32_t argb = 0xC8FF8040u; // A=200, R=255, G=128, B=64
std::cout << "ARGB: 0x" << std::setw(8) << argb << std::endl;
std::uint32_t rgba = Color::argbToRgba(argb);
std::cout << "RGBA: 0x" << std::setw(8) << rgba << std::endl;
// Example 4: Color blending
std::cout << "\n--- Example 4: Alpha Blending ---" << std::endl;
Color foreground{255, 0, 0, 128}; // Semi-transparent red
Color background{0, 0, 255, 255}; // Opaque blue
std::cout << "Foreground: ";
foreground.print();
std::cout << std::endl;
std::cout << "Background: ";
background.print();
std::cout << std::endl;
Color blended = Color::blend(foreground, background);
std::cout << "Blended: ";
blended.print();
std::cout << std::endl;
// Example 5: Transparency check
std::cout << "\n--- Example 5: Transparency Check ---" << std::endl;
const std::uint32_t opaqueColor = Color{255, 255, 255, 255}.pack();
const std::uint32_t transparentColor = Color{255, 255, 255, 200}.pack();
std::cout << "Opaque color (0x" << std::setw(8) << opaqueColor
<< ") has transparency: " << (Color::hasTransparency(opaqueColor) ? "YES" : "NO") << std::endl;
std::cout << "Transparent color (0x" << std::setw(8) << transparentColor
<< ") has transparency: " << (Color::hasTransparency(transparentColor) ? "YES" : "NO") << std::endl;
// Example 6: Color inversion
std::cout << "\n--- Example 6: Color Inversion ---" << std::endl;
constexpr Color original{255, 128, 64, 255};
std::cout << "Original: 0x" << std::setw(8) << original.pack() << " -> ";
original.print();
std::cout << std::endl;
const std::uint32_t inverted = Color::invert(original.pack());
std::cout << "Inverted: 0x" << std::setw(8) << inverted << " -> ";
Color::unpack(inverted).print();
std::cout << std::endl;
// Example 7: Grayscale conversion
std::cout << "\n--- Example 7: Grayscale Conversion ---" << std::endl;
constexpr Color colorful{200, 100, 50, 255};
std::cout << "Colorful: ";
colorful.print();
std::cout << std::endl;
// Convert to grayscale using weighted average
constexpr auto gray =
static_cast<std::uint8_t>(0.299f * colorful.red + 0.587f * colorful.green + 0.114f * colorful.blue);
constexpr Color grayscale{gray, gray, gray, colorful.alpha};
std::cout << "Grayscale: ";
grayscale.print();
std::cout << std::endl;
return 0;
}
constexpr T setBits(T sourceBits, T bitMaskToSet)
Sets specific bits in the source value using a bitmask.
Definition bits.hpp:508
constexpr ValueType shr(ValueType value, ShiftType shift) noexcept
Performs a right logical bit shift operation (>>> in Java).
Definition bits.hpp:403
constexpr T resetBits(T sourceBits, T bitMaskToReset)
Resets (clears) specific bits in a bitmask.
Definition bits.hpp:546
constexpr FirstType orOp(FirstType first, SecondType second) noexcept
Performs a bitwise OR operation between two values of possibly different types and ensures the result...
Definition bits.hpp:437
constexpr FirstType xorOp(FirstType first, SecondType second) noexcept
Performs a bitwise XOR operation between two values of possibly different types and ensures the resul...
Definition bits.hpp:455