| Crates.io | eight-booleans |
| lib.rs | eight-booleans |
| version | 0.2.0 |
| created_at | 2025-12-10 16:24:54.211852+00 |
| updated_at | 2025-12-11 17:48:45.065107+00 |
| description | A small Rust library to store and manipulate 8 booleans in a single byte. |
| homepage | https://github.com/kill-ux/eight-booleans.git |
| repository | https://github.com/kill-ux/eight-booleans.git |
| max_upload_size | |
| id | 1978375 |
| size | 14,232 |
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.
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.
Index type to prevent out-of-range access at compile timeSet 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 the value of a specific bit:
let bit_value = bits.read(0); // Returns true or false
Flip a bit from 1 to 0 or 0 to 1:
bits.toggle(5); // Flip bit 5
Reset all bits to 0:
bits.clear(); // Set all 8 bits to false
Visualize the current state as binary:
bits.display(); // Prints: 00000000 (or current state)
The Index struct ensures type-safe bit indexing:
Index::new(value) - Returns Result<Index, String> (safe, checked)Index::new_unchecked(value) - Panics if out of rangeu8::into() - Converts u8 directly using From traituse 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
}
cargo run
cargo build
cargo test
.
├── Cargo.toml # Project manifest
├── README.md # This file
└── src/
├── lib.rs # Core library (Index, ByteBool)
└── main.rs # Example usage
Index type prevents invalid indices at compile timeSET: Uses OR (|) and AND with NOT (&!)READ: Uses right shift (>>) and AND (&)TOGGLE: Uses XOR (^)CLEAR: Direct assignment to 0ByteBool is a Copy type, enabling efficient passing and manipulationpub 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
pub fn new(value: u8) -> Self // Unchecked construction (panics if the )
pub fn get(&self) -> u8 // Get inner value
This project is open source and available under the MIT License.