eight-booleans

Crates.ioeight-booleans
lib.rseight-booleans
version0.2.0
created_at2025-12-10 16:24:54.211852+00
updated_at2025-12-11 17:48:45.065107+00
descriptionA small Rust library to store and manipulate 8 booleans in a single byte.
homepagehttps://github.com/kill-ux/eight-booleans.git
repositoryhttps://github.com/kill-ux/eight-booleans.git
max_upload_size
id1978375
size14,232
ADNANE EL MIR (Sphinex0)

documentation

https://github.com/kill-ux/eight-booleans.git

README

Eight-Booleans

A Rust library for managing 8 boolean values (bits) in a single byte using bitwise operators. This project demonstrates efficient bit manipulation through a clean, type-safe API.

Overview

ByteBool is a lightweight data structure that packs 8 independent boolean values into a single u8 (byte), allowing you to store and manipulate boolean flags with minimal memory overhead. Each bit in the byte represents one boolean value.

Features

  • Compact Storage: Store 8 booleans in just 1 byte (8 bits)
  • Type-Safe Index: Uses a custom Index type to prevent out-of-range access at compile time
  • Efficient Operations: Uses bitwise operators for O(1) operations
  • Simple API: Clear, intuitive methods for common bit operations

Supported Operations

SET

Set a specific bit to true (1) or false (0):

let mut bits = ByteBool::new();
bits.set(0, true);   // Set bit 0 to true
bits.set(3, false);  // Set bit 3 to false

READ

Read the value of a specific bit:

let bit_value = bits.read(0);  // Returns true or false

TOGGLE

Flip a bit from 1 to 0 or 0 to 1:

bits.toggle(5);  // Flip bit 5

CLEAR

Reset all bits to 0:

bits.clear();  // Set all 8 bits to false

DISPLAY

Visualize the current state as binary:

bits.display();  // Prints: 00000000 (or current state)

Index Type

The Index struct ensures type-safe bit indexing:

  • Valid range: 0-7 (for 8 bits)
  • Construction:
    • Index::new(value) - Returns Result<Index, String> (safe, checked)
    • Index::new_unchecked(value) - Panics if out of range
    • u8::into() - Converts u8 directly using From trait

Usage Example

use eight_booleans::ByteBool;

fn main() {
    let mut bits = ByteBool::default()();
    
    // Set some bits
    bits.set(0, true);
    bits.set(3, true);
    bits.set(7, true);
    bits.display();  // Output: 10001001
    
    // Read a bit
    let is_set = bits.read(0);  // true
    
    // Toggle a bit
    bits.toggle(0);
    bits.display();  // Output: 10001000
    
    // Clear all
    bits.clear();
    bits.display();  // Output: 00000000
}

Building and Running

Run the example:

cargo run

Build the library:

cargo build

Run tests:

cargo test

Project Structure

.
├── Cargo.toml           # Project manifest
├── README.md            # This file
└── src/
    ├── lib.rs           # Core library (Index, ByteBool)
    └── main.rs          # Example usage

Implementation Details

  • Index Validation: The Index type prevents invalid indices at compile time
  • Bitwise Operations:
    • SET: Uses OR (|) and AND with NOT (&!)
    • READ: Uses right shift (>>) and AND (&)
    • TOGGLE: Uses XOR (^)
    • CLEAR: Direct assignment to 0
  • Memory Efficient: ByteBool is a Copy type, enabling efficient passing and manipulation

API Reference

ByteBool

pub fn new() -> Self                                // Create new (all bits 0)
pub fn clear(&mut self)                             // Set all bits to 0
pub fn read(self, index: u8) -> bool                // Read a bit
pub fn set(&mut self, index: u8, new_value: bool)   // Set a bit
pub fn toggle(&mut self, index: u8)                 // Flip a bit
pub fn display(self)                                // Print binary representation

Index

pub fn new(value: u8) -> Self                      // Unchecked construction (panics if the )
pub fn get(&self) -> u8                            // Get inner value

License

This project is open source and available under the MIT License.

Commit count: 0

cargo fmt