10 releases (3 stable)

Uses old Rust 2015

1.1.0 Oct 12, 2018
1.0.0 Jun 2, 2018
0.3.0 Feb 8, 2017
0.1.3 Jun 8, 2016
0.1.0 Sep 17, 2015

#33 in #bit-manipulation

Download history 6071/week @ 2024-05-29 958/week @ 2024-06-05 696/week @ 2024-06-12 499/week @ 2024-06-19 28/week @ 2024-06-26 13/week @ 2024-07-10 10/week @ 2024-07-17 94/week @ 2024-07-24 85/week @ 2024-07-31 59/week @ 2024-08-07 103/week @ 2024-08-14 35/week @ 2024-08-21 22/week @ 2024-08-28 9/week @ 2024-09-04 2/week @ 2024-09-11

72 downloads per month
Used in tak

MIT/Apache

13KB
229 lines

twiddle

A small library for bit-twiddling.

Documentation

Usage

You can use it by adding this to your Cargo.toml file:

[dependencies]
twiddle = "1.1"

and adding this to the top of your crate root (main.rs or lib.rs):

extern crate twiddle;

use twiddle::Twiddle;

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 = f.to_bits();
        UnpackedF32 {
            negative: b.bit(31),
            exponent: (b.bits(30..=23) as i16) - 127,
            fraction: b.bits(22..=0)
        }
    }
}

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

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps