2 unstable releases
0.2.0 | Sep 29, 2020 |
---|---|
0.1.2 | Feb 17, 2020 |
0.1.1 |
|
0.1.0 |
|
#183 in No standard library
7,679 downloads per month
Used in 17 crates
(2 directly)
14KB
330 lines
intbits
Easy access to individual bits of integers
use intbits::Bits;
assert_eq!(2.bit(0), false);
assert_eq!(2.bit(1), true);
assert_eq!(2.bit(2), false);
assert_eq!(0b1011u32.bits(0..2), 0b11);
assert_eq!(0b1011u32.bits(2..4), 0b10);
assert_eq!(0xFFu8.with_bit(3, false), 0xF7);
assert_eq!(0xFFu8.with_bits(4..8, 3), 0x3F);
See the documentation.
lib.rs
:
This crates provides two functions for accessing the individual bits of integers:
.bit(i)
to get one specific bit..bits(i..j)
to get a range of bits.
It also provides similar functions for changing specific bits of integers:
.set_bit(i, bit)
to set one specific bit..set_bits(i..j, bits)
to set a range of bits.
These variants return a new integer, instead of modifying it:
Example
use intbits::Bits;
assert_eq!(2.bit(0), false);
assert_eq!(2.bit(1), true);
assert_eq!(2.bit(2), false);
assert_eq!(0b1011u32.bits(0..2), 0b11);
assert_eq!(0b1011u32.bits(2..4), 0b10);
assert_eq!(0xFFu8.with_bit(3, false), 0xF7);
assert_eq!(0xFFu8.with_bits(4..8, 3), 0x3F);