From 3ca3c948cf3e16a3e4dd8ce50811615eb2a8f18a Mon Sep 17 00:00:00 2001 From: nikolajbjorner Date: Fri, 27 Feb 2015 11:08:49 -0800 Subject: [PATCH] add bit-vector extract shortcuts to C++ API Signed-off-by: nikolajbjorner --- examples/c++/example.cpp | 10 ++++++++++ src/api/c++/z3++.h | 3 +++ 2 files changed, 13 insertions(+) diff --git a/examples/c++/example.cpp b/examples/c++/example.cpp index 0d8a3ceb3..e48c23905 100644 --- a/examples/c++/example.cpp +++ b/examples/c++/example.cpp @@ -975,6 +975,15 @@ void substitute_example() { std::cout << new_f << std::endl; } +void extract_example() { + std::cout << "extract example\n"; + context c; + expr x(c); + x = c.constant("x", c.bv_sort(32)); + expr y = x.extract(21, 10); + std::cout << y << " " << y.hi() << " " << y.lo() << "\n"; +} + int main() { try { demorgan(); std::cout << "\n"; @@ -1013,6 +1022,7 @@ int main() { expr_vector_example(); std::cout << "\n"; exists_expr_vector_example(); std::cout << "\n"; substitute_example(); std::cout << "\n"; + extract_example(); std::cout << "\n"; std::cout << "done\n"; } catch (exception & ex) { diff --git a/src/api/c++/z3++.h b/src/api/c++/z3++.h index 7380e52f1..f43ba83e5 100644 --- a/src/api/c++/z3++.h +++ b/src/api/c++/z3++.h @@ -866,6 +866,9 @@ namespace z3 { friend expr operator|(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) | b; } friend expr operator~(expr const & a) { Z3_ast r = Z3_mk_bvnot(a.ctx(), a); return expr(a.ctx(), r); } + expr extract(unsigned hi, unsigned lo) const { Z3_ast r = Z3_mk_extract(ctx(), hi, lo, *this); return expr(ctx(), r); } + unsigned lo() const { assert (is_app() && Z3_get_decl_num_parameters(ctx(), decl()) == 2); return static_cast(Z3_get_decl_int_parameter(ctx(), decl(), 1)); } + unsigned hi() const { assert (is_app() && Z3_get_decl_num_parameters(ctx(), decl()) == 2); return static_cast(Z3_get_decl_int_parameter(ctx(), decl(), 0)); } /** \brief Return a simplified version of this expression.