Giter Site home page Giter Site logo

phip1611 / bit_ops Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 1.0 67 KB

Bitwise operations on primitive integer types, but no_std and const-compatible. Provides a collection of typical bit manipulation operations that are primarily required in low-level development. Unlike other crates that provide tooling to create sophisticated high-level types with bitfields, the focus of bit_ops is to work on raw integers.

License: MIT License

Rust 100.00%
bitwise rust

bit_ops's Introduction

bit_ops

Common bit-oriented operations on primitive integer types with a focus on no_std and const compatibility. Unlike other crates that provide tooling to create sophisticated high-level types with bitfields, the focus of bit_ops is on raw primitive integer types.

Documentation

See https://docs.rs/bit_ops.

Example

fn main() {
    // PREREQUISITES: Some Definitions

    /// See specification of the x86 IOAPIC redirection entry for more details.
    mod x86_ioapic {
        pub const VECTOR_BITS: u64 = 8;
        pub const VECTOR_SHIFT: u64 = 0;
        pub const DELIVERY_MODE_BITS: u64 = 3;
        pub const DELIVERY_MODE_SHIFT: u64 = 8;
        pub const DESTINATION_MODE_BITS: u64 = 1;
        pub const DESTINATION_MODE_SHIFT: u64 = 11;
        pub const PIN_POLARITY_BITS: u64 = 1;
        pub const PIN_POLARITY_SHIFT: u64 = 13;
        pub const TRIGGER_MODE_BITS: u64 = 1;
        pub const TRIGGER_MODE_SHIFT: u64 = 15;
        pub const MASKED_BITS: u64 = 1;
        pub const MASKED_SHIFT: u64 = 16;
        pub const DESTINATION_BITS: u64 = 8;
        pub const DESTINATION_SHIFT: u64 = 56;
    }

    use x86_ioapic::*;

    // ACTUAL LIBRARY USAGE BEGINS HERE

    let redirection_entry = bit_ops::bitops_u64::set_bits_exact_n(
        0,
        &[
            (7, VECTOR_BITS, VECTOR_SHIFT),
            (0b111 /* ExtInt */, DELIVERY_MODE_BITS, DELIVERY_MODE_SHIFT),
            (0 /* physical */, DESTINATION_MODE_BITS, DESTINATION_MODE_SHIFT),
            (1 /* low-active */, PIN_POLARITY_BITS, PIN_POLARITY_SHIFT),
            (1 /* level-triggered */, TRIGGER_MODE_BITS, TRIGGER_MODE_SHIFT),
            (1 /* masked */, MASKED_BITS, MASKED_SHIFT),
            (13 /* APIC ID */, DESTINATION_BITS, DESTINATION_SHIFT),
        ],
    );
    assert_eq!(redirection_entry, 0xd0000000001a707);
}

MSRV

1.57.0 stable

License

MIT License. See LICENSE file.

bit_ops's People

Contributors

phip1611 avatar dependabot[bot] avatar

Stargazers

 avatar Jevin Sweval avatar

Watchers

Jevin Sweval avatar  avatar  avatar

Forkers

john-k

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.