Crate twiddle [] [src]

A library for various bit-twiddling utility functions.

NOTE: Most of the functions in this library take ranges of the form 7..4. These are inclusive ranges, not exclusive, signifying the bits 7 through 4 including 4.

Example

extern crate twiddle;

use twiddle::Twiddle;

struct UnpackedF32 {
    negative: bool,
    exponent: i16,
    fraction: u32,
}

impl From<f32> for UnpackedF32 {
    fn from(f: f32) -> UnpackedF32 {
        let b = unsafe { std::mem::transmute::<f32, u32>(f) };
        UnpackedF32 {
            negative: b.bit(31),
            exponent: (b.bits(30..23) as i16) - 127,
            fraction: b.bits(22..0)
        }
    }
}

fn main() {
    for f in (-5..6) {
        let u = UnpackedF32::from(f as f32);
        println!("{:+} = {}1.{:023b} * 2^{}",
            f,
            match u.negative { false => "+", true => "-" },
            u.fraction,
            u.exponent
        );
    }
}

Structs

Split

An iterator over sets of bits. See Twiddle::split() for more information.

Traits

Twiddle

A trait for bit-twiddling utility functions.