Skip to main content

BitVec

Struct BitVec 

Source
pub struct BitVec { /* private fields */ }
Expand description

An exact W-bit value, W in 1..=512.

Implementations§

Source§

impl BitVec

Source

pub fn zero(width: Width) -> Self

Zero.

Source

pub fn one(width: Width) -> Self

One.

Source

pub fn ones(width: Width) -> Self

All ones (-1 as a signed value).

Source

pub fn smin(width: Width) -> Self

The most negative signed value (only the top bit set).

Source

pub fn smax(width: Width) -> Self

The most positive signed value (every bit but the top set).

Source

pub fn from_u64(width: Width, v: u64) -> Result<Self, ValueError>

v, which must fit in width bits as an unsigned value.

Source

pub fn from_u128(width: Width, v: u128) -> Result<Self, ValueError>

v, which must fit in width bits as an unsigned value.

Source

pub fn from_i128(width: Width, v: i128) -> Result<Self, ValueError>

v, which must be representable as a width-bit signed value.

Source

pub fn wrapping_from_u64(width: Width, v: u64) -> Self

v truncated to width bits.

Source

pub fn wrapping_from_u128(width: Width, v: u128) -> Self

v truncated to width bits.

Source

pub fn wrapping_from_i128(width: Width, v: i128) -> Self

v in two’s complement, sign-extended or truncated to width bits.

Source

pub fn from_limbs(width: Width, limbs: &[u64]) -> Result<Self, ValueError>

Little-endian limbs, which must not have bits set at or above width.

Source

pub fn wrapping_from_limbs(width: Width, limbs: &[u64]) -> Self

Little-endian limbs truncated to width bits.

Source

pub fn width(&self) -> Width

The width.

Source

pub fn limbs(&self) -> &[u64]

The ceil(W / 64) active little-endian limbs.

Source

pub fn to_u64(&self) -> Option<u64>

The unsigned value, if it fits in a u64.

Source

pub fn to_u128(&self) -> Option<u128>

The unsigned value, if it fits in a u128.

Source

pub fn to_i128(&self) -> Option<i128>

The signed (two’s-complement) value, if it fits in an i128.

Source

pub fn bit(&self, i: u16) -> Option<bool>

Bit i, or None if i >= W.

Source

pub fn msb(&self) -> bool

The sign (most significant) bit.

Source

pub fn is_zero(&self) -> bool

Whether the value is zero.

Source

pub fn is_ones(&self) -> bool

Whether every bit is set.

Source

pub fn apply_un(op: UnOp, a: &BitVec) -> Result<BitVec, WidthError>

Applies a unary operator. The only error is Bswap at a width that is not a multiple of 8.

Source

pub fn apply_bin( op: BinOp, a: &BitVec, b: &BitVec, ) -> Result<BitVec, WidthError>

Applies a binary operator. Both operands must have the same width.

Source

pub fn apply_cmp( op: impl Into<CmpOpExt>, a: &BitVec, b: &BitVec, ) -> Result<bool, WidthError>

Evaluates a comparison. Both operands must have the same width.

Source

pub fn zext(&self, to: Width) -> Result<BitVec, WidthError>

Zero extension to to, which must be at least as wide (equal width is the identity).

Source

pub fn sext(&self, to: Width) -> Result<BitVec, WidthError>

Sign extension to to, which must be at least as wide (equal width is the identity).

Source

pub fn trunc(&self, to: Width) -> Result<BitVec, WidthError>

The low to bits (to must not exceed the width).

Source

pub fn extract(&self, lo: u16, len: Width) -> Result<BitVec, WidthError>

Bits [lo, lo + len).

Source

pub fn concat(hi: &BitVec, lo: &BitVec) -> Result<BitVec, WidthError>

hi in the high bits and lo in the low bits.

Source

pub fn select(c: &BitVec, t: &BitVec, f: &BitVec) -> Result<BitVec, WidthError>

if c { t } else { f } for a 1-bit condition.

Source

pub fn from_bool(b: bool) -> BitVec

A 1-bit value from a boolean.

Source

pub fn parse(s: &str) -> Result<BitVec, ParseError>

Parses <digits>:<width>, where digits are decimal (optionally negative), 0x hex or 0b binary, or the Verilog form <width>'h<hex> / 'd / 'b.

use bitwright::{BitVec, Width};
assert_eq!(BitVec::parse("0xff:8").unwrap(), BitVec::ones(Width::W8));
assert_eq!(BitVec::parse("-1:32").unwrap(), BitVec::ones(Width::W32));
assert_eq!(BitVec::parse("8'h80").unwrap(), BitVec::smin(Width::W8));

Trait Implementations§

Source§

impl Clone for BitVec

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for BitVec

Source§

impl Debug for BitVec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for BitVec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

0x<hex>:<width>, the form accepted by BitVec::parse.

Source§

impl Eq for BitVec

Source§

impl FromStr for BitVec

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, ParseError>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for BitVec

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl LowerHex for BitVec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

The value in lower-case hexadecimal, without prefix or width.

Source§

impl Ord for BitVec

Source§

fn cmp(&self, other: &Self) -> Ordering

By width, then by unsigned value. (A total order for sorting and canonical keys; use BitVec::apply_cmp for signed or unsigned comparison of same-width values.)

1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for BitVec

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for BitVec

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for BitVec

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.