pub struct SignedBits<const N: usize>(/* private fields */);
Expand description
The SignedBits type is a fixed-size bit vector. It is meant to imitate the behavior of signed bit vectors in hardware. Due to the design of the SignedBits type, you can only create a signed bit vector of up to 128 bits in lnegth for now. However, you can easily express larger constructs in hardware using arrays, tuples and structs. The only real limitation of the SignedBits type being 128 bits is that you cannot perform arbitrary arithmetic on longer bit values in your hardware designs.
Signed arithmetic is performed using 2’s complement arithmetic. See https://en.wikipedia.org/wiki/Two%27s_complement for more information.
Note that unlike the Bits type, comparisons are performed using signed arithmetic. Note also that the right shift operator when applied to a signed value will sign extend the value. This is the same behavior as is seen in Rust (i.e., ((-4) >> 1) == -2).
If you want to right shift a signed value without sign extension, then you should convert it to a Bits type first.
Implementations§
Source§impl<const N: usize> SignedBits<N>
impl<const N: usize> SignedBits<N>
Sourcepub const MASK: Self
pub const MASK: Self
Return a SignedBits value with all bits set to 1.
Sourcepub const fn mask() -> Self
pub const fn mask() -> Self
Return a SignedBits value with all bits set to 1.
let mask = SignedBits::<8>::mask();
assert_eq!(mask.as_unsigned(), 0xFF);
Note that for a SignedBits value, the mask is the same as a representation of -1.
Sourcepub fn sign_bit(&self) -> bool
pub fn sign_bit(&self) -> bool
Extract the sign bit from the SignedBits value. By convention for 2’s complement arithmetic, this is true if the value is negative.
let positive = SignedBits::<8>::from(1);
assert!(!positive.sign_bit());
let negative = SignedBits::<8>::from(-1);
assert!(negative.sign_bit());
Sourcepub fn max_value() -> i128
pub fn max_value() -> i128
Return the largest positive value that can be represented by this sized SignedBits value.
assert_eq!(SignedBits::<8>::max_value(), i8::MAX as i128);
Sourcepub fn min_value() -> i128
pub fn min_value() -> i128
Return the smallest negative value that can be represented by this sized SignedBits value.
assert_eq!(SignedBits::<8>::min_value(), i8::MIN as i128);
Sourcepub fn set_bit(&mut self, bit: usize, value: bool)
pub fn set_bit(&mut self, bit: usize, value: bool)
Set a specific bit of a SignedBits value to 1 or 0. Note that changing the MSB of a signed bit vector changes the sign of that vector.
let mut value = SignedBits::<8>::from(0);
value.set_bit(0, true);
assert_eq!(value, 1);
value.set_bit(7, true);
assert_eq!(value, -127);
Sourcepub fn get_bit(&self, bit: usize) -> bool
pub fn get_bit(&self, bit: usize) -> bool
Get the value of a specific bit of a SignedBits value.
let mut value = SignedBits::<4>::from(5);
assert!(value.get_bit(0));
assert!(!value.get_bit(1));
assert!(value.get_bit(2));
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Test if the value is negative.
assert!(signed::<8>(-1).is_negative());
assert!(!signed::<8>(0).is_negative());
assert!(!signed::<8>(1).is_negative());
Sourcepub fn is_non_negative(&self) -> bool
pub fn is_non_negative(&self) -> bool
Test if the value is positive or zero.
assert!(!signed::<8>(-1).is_non_negative());
assert!(signed::<8>(0).is_non_negative());
assert!(signed::<8>(1).is_non_negative());
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 SignedBits value. Because we cannot guarantee that the sliced bits include the proper 2’s complement representation for a signed value, they are simple a Bits vector.
let x = signed::<8>(-14); // In binary: 1111_0010
let y = x.slice::<4>(0);
assert_eq!(y, 0b0010);
Sourcepub fn as_unsigned(self) -> Bits<N>
pub fn as_unsigned(self) -> Bits<N>
Reinterpret the SignedBits value as an unsigned Bits value. This is useful for performing bit manipulations on the value that may or not preserve the 2’s complement nature of the value.
let x = signed::<8>(-14); // In binary: 1111_0010
let y : Bits<8> = x.as_unsigned();
assert_eq!(y, 0b1111_0010);
Trait Implementations§
Source§impl<const N: usize> Add<SignedBits<N>> for i128
impl<const N: usize> Add<SignedBits<N>> for i128
Source§type Output = SignedBits<N>
type Output = SignedBits<N>
+
operator.Source§impl<const N: usize> Add for SignedBits<N>
impl<const N: usize> Add for SignedBits<N>
Source§impl<const N: usize> AddAssign<i128> for SignedBits<N>
impl<const N: usize> AddAssign<i128> for SignedBits<N>
Source§fn add_assign(&mut self, rhs: i128)
fn add_assign(&mut self, rhs: i128)
+=
operation. Read moreSource§impl<const N: usize> AddAssign for SignedBits<N>
impl<const N: usize> AddAssign for SignedBits<N>
Source§fn add_assign(&mut self, rhs: SignedBits<N>)
fn add_assign(&mut self, rhs: SignedBits<N>)
+=
operation. Read moreSource§impl<const N: usize> Binary for SignedBits<N>
impl<const N: usize> Binary for SignedBits<N>
Source§impl<const N: usize> BitAnd<SignedBits<N>> for i128
impl<const N: usize> BitAnd<SignedBits<N>> for i128
Source§type Output = SignedBits<N>
type Output = SignedBits<N>
&
operator.Source§impl<const N: usize> BitAnd for SignedBits<N>
impl<const N: usize> BitAnd for SignedBits<N>
Source§type Output = SignedBits<N>
type Output = SignedBits<N>
&
operator.Source§fn bitand(self, rhs: SignedBits<N>) -> SignedBits<N>
fn bitand(self, rhs: SignedBits<N>) -> SignedBits<N>
&
operation. Read moreSource§impl<const N: usize> BitAndAssign<i128> for SignedBits<N>
impl<const N: usize> BitAndAssign<i128> for SignedBits<N>
Source§fn bitand_assign(&mut self, rhs: i128)
fn bitand_assign(&mut self, rhs: i128)
&=
operation. Read moreSource§impl<const N: usize> BitAndAssign for SignedBits<N>
impl<const N: usize> BitAndAssign for SignedBits<N>
Source§fn bitand_assign(&mut self, rhs: SignedBits<N>)
fn bitand_assign(&mut self, rhs: SignedBits<N>)
&=
operation. Read moreSource§impl<const N: usize> BitOr<SignedBits<N>> for i128
impl<const N: usize> BitOr<SignedBits<N>> for i128
Source§type Output = SignedBits<N>
type Output = SignedBits<N>
|
operator.Source§impl<const N: usize> BitOr for SignedBits<N>
impl<const N: usize> BitOr for SignedBits<N>
Source§type Output = SignedBits<N>
type Output = SignedBits<N>
|
operator.Source§fn bitor(self, rhs: SignedBits<N>) -> SignedBits<N>
fn bitor(self, rhs: SignedBits<N>) -> SignedBits<N>
|
operation. Read moreSource§impl<const N: usize> BitOrAssign<i128> for SignedBits<N>
impl<const N: usize> BitOrAssign<i128> for SignedBits<N>
Source§fn bitor_assign(&mut self, rhs: i128)
fn bitor_assign(&mut self, rhs: i128)
|=
operation. Read moreSource§impl<const N: usize> BitOrAssign for SignedBits<N>
impl<const N: usize> BitOrAssign for SignedBits<N>
Source§fn bitor_assign(&mut self, rhs: SignedBits<N>)
fn bitor_assign(&mut self, rhs: SignedBits<N>)
|=
operation. Read moreSource§impl<const N: usize> BitXor<SignedBits<N>> for i128
impl<const N: usize> BitXor<SignedBits<N>> for i128
Source§type Output = SignedBits<N>
type Output = SignedBits<N>
^
operator.Source§impl<const N: usize> BitXor for SignedBits<N>
impl<const N: usize> BitXor for SignedBits<N>
Source§type Output = SignedBits<N>
type Output = SignedBits<N>
^
operator.Source§fn bitxor(self, rhs: SignedBits<N>) -> SignedBits<N>
fn bitxor(self, rhs: SignedBits<N>) -> SignedBits<N>
^
operation. Read moreSource§impl<const N: usize> BitXorAssign<i128> for SignedBits<N>
impl<const N: usize> BitXorAssign<i128> for SignedBits<N>
Source§fn bitxor_assign(&mut self, rhs: i128)
fn bitxor_assign(&mut self, rhs: i128)
^=
operation. Read moreSource§impl<const N: usize> BitXorAssign for SignedBits<N>
impl<const N: usize> BitXorAssign for SignedBits<N>
Source§fn bitxor_assign(&mut self, rhs: SignedBits<N>)
fn bitxor_assign(&mut self, rhs: SignedBits<N>)
^=
operation. Read moreSource§impl<const N: usize> Clone for SignedBits<N>
impl<const N: usize> Clone for SignedBits<N>
Source§fn clone(&self) -> SignedBits<N>
fn clone(&self) -> SignedBits<N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<const N: usize> Debug for SignedBits<N>
impl<const N: usize> Debug for SignedBits<N>
Source§impl<const N: usize> Default for SignedBits<N>
impl<const N: usize> Default for SignedBits<N>
Source§impl<const N: usize> Display for SignedBits<N>
impl<const N: usize> Display for SignedBits<N>
Source§impl<const N: usize> Hash for SignedBits<N>
impl<const N: usize> Hash for SignedBits<N>
Source§impl<const N: usize> LowerHex for SignedBits<N>
impl<const N: usize> LowerHex for SignedBits<N>
Source§impl<const N: usize> Neg for SignedBits<N>
impl<const N: usize> Neg for SignedBits<N>
Source§impl<const N: usize> Not for SignedBits<N>
impl<const N: usize> Not for SignedBits<N>
Source§impl<const N: usize> Ord for SignedBits<N>
impl<const N: usize> Ord for SignedBits<N>
Source§fn cmp(&self, other: &SignedBits<N>) -> Ordering
fn cmp(&self, other: &SignedBits<N>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const N: usize> PartialEq for SignedBits<N>
impl<const N: usize> PartialEq for SignedBits<N>
Source§impl<const N: usize> PartialOrd for SignedBits<N>
impl<const N: usize> PartialOrd for SignedBits<N>
Source§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 SignedBits<N>
impl<const N: usize> ShlAssign<u128> for SignedBits<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 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 SignedBits<N>
impl<const N: usize> ShrAssign<u128> for SignedBits<N>
Source§fn shr_assign(&mut self, rhs: u128)
fn shr_assign(&mut self, rhs: u128)
>>=
operation. Read moreSource§impl<const N: usize> Sub for SignedBits<N>
impl<const N: usize> Sub for SignedBits<N>
Source§impl<const N: usize> SubAssign<i128> for SignedBits<N>
impl<const N: usize> SubAssign<i128> for SignedBits<N>
Source§fn sub_assign(&mut self, rhs: i128)
fn sub_assign(&mut self, rhs: i128)
-=
operation. Read moreSource§impl<const N: usize> SubAssign for SignedBits<N>
impl<const N: usize> SubAssign for SignedBits<N>
Source§fn sub_assign(&mut self, rhs: SignedBits<N>)
fn sub_assign(&mut self, rhs: SignedBits<N>)
-=
operation. Read more