Skip to content

Commit

Permalink
add is_all_ones function
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiwi committed Nov 18, 2024
1 parent 5ed8de9 commit 2583065
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
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.1"
version = "0.14.2"
edition = "2021"
authors = ["Kevin Laeufer <laeufer@cornell.edu>"]
description = "BitVector and Array Arithmetic"
Expand Down
24 changes: 24 additions & 0 deletions src/bv/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ pub trait BitVecOps {
self.words().iter().all(|w| *w == 0)
}

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

fn is_negative(&self) -> bool {
crate::bv::arithmetic::is_neg(self.words(), self.width())
}
Expand Down Expand Up @@ -582,4 +591,19 @@ mod tests {
let value = src_ref.to_u64().unwrap();
assert_eq!(value, 1111111111 * 2);
}

#[test]
fn test_is_all_ones() {
let mut a = BitVecValue::ones(23);
assert!(a.is_all_ones());
a.clear_bit(20);
assert!(!a.is_all_ones());

let mut a = BitVecValue::ones(1345);
assert!(a.is_all_ones());
a.clear_bit(1000);
assert!(!a.is_all_ones());
a.set_bit(1000);
assert!(a.is_all_ones());
}
}

0 comments on commit 2583065

Please sign in to comment.