| Crates.io | crispii_bits |
| lib.rs | crispii_bits |
| version | 1.0.1 |
| created_at | 2025-03-22 19:35:18.853243+00 |
| updated_at | 2025-09-17 18:36:43.076022+00 |
| description | Bit-related functionality for Rust's native u{int} types (usize excluded) |
| homepage | |
| repository | https://github.com/stephen-paton/crispii_bits |
| max_upload_size | |
| id | 1602044 |
| size | 65,061 |
Bit operations made safe.
Useful for situations where you're emulating CPU registers or need to carry out bit-level operations on Rust's native unsigned integer types (usize excluded).
Provides three extension methods for each:
add_with_carry -> Returns a tuple containing the resulting u{int} value, and a Vec of all of the bit positions where carries occurred (the bit that overflowed to the next bit on the left, not the destination bit of the overlow)set_bit -> Sets the bit on (1) or off (0) at the position specifiedflip_bit -> Flips the bit (0 -> 1 || 1 -> 0) at the position specifiedEach of these methods uses a respective PosU{int} enum for bit selection, guaranteeing safety, though the try_into method is available on the u8 type if desired.
The former approach is less cumbersome in practice:
use crispii_bits::u8::*;
let result: u8 = 0b0100_0000.flip_bit(PosU8::B6);
vs.
use crispii_bits::u8::*;
let result: u8 = 0b0100_0000.flip_bit(6.try_into().unwrap());
Importing the crate is as simple as importing * from the u{int} module that matches the native u{int} type you want to use.
For example, a u16:
use crispii_bits::u16::*;
License: MIT OR Apache-2.0