Skip to content

Commit

Permalink
add is_one method
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiwi committed Nov 19, 2024
1 parent 659cb7f commit 01ec209
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "baa"
version = "0.14.2"
version = "0.14.3"
edition = "2021"
authors = ["Kevin Laeufer <[email protected]>"]
description = "BitVector and Array Arithmetic"
Expand Down
19 changes: 17 additions & 2 deletions src/bv/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,18 @@ pub trait BitVecOps {
self.words().iter().all(|w| *w == 0)
}

fn is_one(&self) -> bool {
let msbs_are_zero = self.words().iter().skip(1).all(|w| *w == 0);
msbs_are_zero && self.words()[0] == 1
}

fn is_all_ones(&self) -> bool {
let lsbs_zero = self
let lsbs_are_max = self
.words()
.iter()
.take(self.words().len() - 1)
.all(|w| *w == Word::MAX);
lsbs_zero && (*self.words().last().unwrap() == mask(self.width() % Word::BITS))
lsbs_are_max && (*self.words().last().unwrap() == mask(self.width() % Word::BITS))
}

fn is_negative(&self) -> bool {
Expand Down Expand Up @@ -606,4 +611,14 @@ mod tests {
a.set_bit(1000);
assert!(a.is_all_ones());
}

#[test]
fn test_is_one() {
let mut a = BitVecValue::zero(23);
assert!(!a.is_one());
a.set_bit(0);
assert!(a.is_one());
a.set_bit(20);
assert!(!a.is_one());
}
}

0 comments on commit 01ec209

Please sign in to comment.