From 01ec20933dcfd569961ae0a82ada772ec6456096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20L=C3=A4ufer?= Date: Tue, 19 Nov 2024 15:55:28 -0500 Subject: [PATCH] add is_one method --- Cargo.toml | 2 +- src/bv/ops.rs | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5ef6e34..cd13ec6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "baa" -version = "0.14.2" +version = "0.14.3" edition = "2021" authors = ["Kevin Laeufer "] description = "BitVector and Array Arithmetic" diff --git a/src/bv/ops.rs b/src/bv/ops.rs index 7b32fb9..01cd4b9 100644 --- a/src/bv/ops.rs +++ b/src/bv/ops.rs @@ -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 { @@ -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()); + } }