pub struct Bits<const N: usize>(/* private fields */);
Expand description
The Bits type is a fixed-sized bit vector. It is meant to
imitate the behavior of bit vectors in hardware. Due to the
design of the Bits type, you can only create a Bits type of
up to 128 bits in length for now. However, you can easily express
larger constructs in hardware using arrays, tuples and structs.
The only real limitation of the Bits type being 128 bits is that
you cannot perform arbitrary arithmetic on longer bit values in your
hardware designs. I don’t think this is a significant issue, but
the Bits design of the rust-hdl
crate was much slower and harder
to maintain and use. I think this is a good trade-off.
Note that the Bits type implements 2’s complement arithmetic. See https://en.wikipedia.org/wiki/Two%27s_complement for more information.
Note also that the Bits kind is treated as an unsigned value for the purposes of comparisons. If you need signed comparisons, you will need the SignedBits type.
Implementations§
Source§impl<const N: usize> Bits<N>
impl<const N: usize> Bits<N>
Sourcepub const fn mask() -> Self
pub const fn mask() -> Self
Return a Bits value with all bits set to 1.
let bits = Bits::<8>::mask();
assert_eq!(bits, 0xFF);
Sourcepub fn any(self) -> bool
pub fn any(self) -> bool
Returns true if any of the bits are set to 1.
let bits : Bits<8> = 0b1101_1010.into();
assert!(bits.any());
let bits : Bits<8> = 0.into();
assert!(!bits.any());
Sourcepub fn all(self) -> bool
pub fn all(self) -> bool
Returns true if all of the bits are set to 1.
let bits : Bits<8> = 0b1101_1010.into();
assert!(!bits.all());
let bits : Bits<8> = Bits::mask();
assert!(bits.all());
Sourcepub fn slice<const M: usize>(&self, start: usize) -> Bits<M>
pub fn slice<const M: usize>(&self, start: usize) -> Bits<M>
Extracts a range of bits from the Bits value.
Sourcepub fn as_signed(self) -> SignedBits<N>
pub fn as_signed(self) -> SignedBits<N>
Reinterpret the Bits value as a SignedBits value.
Trait Implementations§
Source§impl<const N: usize> AddAssign<u128> for Bits<N>
impl<const N: usize> AddAssign<u128> for Bits<N>
Source§fn add_assign(&mut self, rhs: u128)
fn add_assign(&mut self, rhs: u128)
+=
operation. Read moreSource§impl<const N: usize> AddAssign for Bits<N>
impl<const N: usize> AddAssign for Bits<N>
Source§fn add_assign(&mut self, rhs: Bits<N>)
fn add_assign(&mut self, rhs: Bits<N>)
+=
operation. Read moreSource§impl<const N: usize> BitAndAssign<u128> for Bits<N>
impl<const N: usize> BitAndAssign<u128> for Bits<N>
Source§fn bitand_assign(&mut self, rhs: u128)
fn bitand_assign(&mut self, rhs: u128)
&=
operation. Read moreSource§impl<const N: usize> BitAndAssign for Bits<N>
impl<const N: usize> BitAndAssign for Bits<N>
Source§fn bitand_assign(&mut self, rhs: Bits<N>)
fn bitand_assign(&mut self, rhs: Bits<N>)
&=
operation. Read moreSource§impl<const N: usize> BitOrAssign<u128> for Bits<N>
impl<const N: usize> BitOrAssign<u128> for Bits<N>
Source§fn bitor_assign(&mut self, rhs: u128)
fn bitor_assign(&mut self, rhs: u128)
|=
operation. Read moreSource§impl<const N: usize> BitOrAssign for Bits<N>
impl<const N: usize> BitOrAssign for Bits<N>
Source§fn bitor_assign(&mut self, rhs: Bits<N>)
fn bitor_assign(&mut self, rhs: Bits<N>)
|=
operation. Read moreSource§impl<const N: usize> BitXorAssign<u128> for Bits<N>
impl<const N: usize> BitXorAssign<u128> for Bits<N>
Source§fn bitxor_assign(&mut self, rhs: u128)
fn bitxor_assign(&mut self, rhs: u128)
^=
operation. Read moreSource§impl<const N: usize> BitXorAssign for Bits<N>
impl<const N: usize> BitXorAssign for Bits<N>
Source§fn bitxor_assign(&mut self, rhs: Bits<N>)
fn bitxor_assign(&mut self, rhs: Bits<N>)
^=
operation. Read moreSource§impl<const N: usize> Ord for Bits<N>
impl<const N: usize> Ord for Bits<N>
Source§impl<const N: usize> PartialOrd for Bits<N>
impl<const N: usize> PartialOrd for Bits<N>
Source§impl<const M: usize, const N: usize> ShlAssign<Bits<M>> for Bits<N>
impl<const M: usize, const N: usize> ShlAssign<Bits<M>> for Bits<N>
Source§fn shl_assign(&mut self, rhs: Bits<M>)
fn shl_assign(&mut self, rhs: Bits<M>)
<<=
operation. Read moreSource§impl<const M: usize, const N: usize> ShlAssign<Bits<M>> for SignedBits<N>
impl<const M: usize, const N: usize> ShlAssign<Bits<M>> for SignedBits<N>
Source§fn shl_assign(&mut self, rhs: Bits<M>)
fn shl_assign(&mut self, rhs: Bits<M>)
<<=
operation. Read moreSource§impl<const N: usize> ShlAssign<u128> for Bits<N>
impl<const N: usize> ShlAssign<u128> for Bits<N>
Source§fn shl_assign(&mut self, rhs: u128)
fn shl_assign(&mut self, rhs: u128)
<<=
operation. Read moreSource§impl<const M: usize, const N: usize> ShrAssign<Bits<M>> for Bits<N>
impl<const M: usize, const N: usize> ShrAssign<Bits<M>> for Bits<N>
Source§fn shr_assign(&mut self, rhs: Bits<M>)
fn shr_assign(&mut self, rhs: Bits<M>)
>>=
operation. Read moreSource§impl<const M: usize, const N: usize> ShrAssign<Bits<M>> for SignedBits<N>
impl<const M: usize, const N: usize> ShrAssign<Bits<M>> for SignedBits<N>
Source§fn shr_assign(&mut self, rhs: Bits<M>)
fn shr_assign(&mut self, rhs: Bits<M>)
>>=
operation. Read moreSource§impl<const N: usize> ShrAssign<u128> for Bits<N>
impl<const N: usize> ShrAssign<u128> for Bits<N>
Source§fn shr_assign(&mut self, rhs: u128)
fn shr_assign(&mut self, rhs: u128)
>>=
operation. Read moreSource§impl<const N: usize> SubAssign<u128> for Bits<N>
impl<const N: usize> SubAssign<u128> for Bits<N>
Source§fn sub_assign(&mut self, rhs: u128)
fn sub_assign(&mut self, rhs: u128)
-=
operation. Read moreSource§impl<const N: usize> SubAssign for Bits<N>
impl<const N: usize> SubAssign for Bits<N>
Source§fn sub_assign(&mut self, rhs: Bits<N>)
fn sub_assign(&mut self, rhs: Bits<N>)
-=
operation. Read more