Theory of Bit-Vectors: extract

examples/api/cpp/extract.cpp

 1/******************************************************************************
 2 * This file is part of the cvc5 project.
 3 *
 4 * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
 5 * in the top-level source directory and their institutional affiliations.
 6 * All rights reserved.  See the file COPYING in the top-level source
 7 * directory for licensing information.
 8 * ****************************************************************************
 9 *
10 * A simple demonstration of the solving capabilities of the cvc5
11 * bit-vector solver.
12 *
13 */
14
15#include <cvc5/cvc5.h>
16
17#include <iostream>
18
19using namespace std;
20using namespace cvc5;
21
22int main()
23{
24  TermManager tm;
25  Solver slv(tm);
26  slv.setLogic("QF_BV"); // Set the logic
27
28  Sort bv32 = tm.mkBitVectorSort(32);
29
30  Term x = tm.mkConst(bv32, "x");
31
32  Op ext_31_1 = tm.mkOp(Kind::BITVECTOR_EXTRACT, {31, 1});
33  Term x_31_1 = tm.mkTerm(ext_31_1, {x});
34
35  Op ext_30_0 = tm.mkOp(Kind::BITVECTOR_EXTRACT, {30, 0});
36  Term x_30_0 = tm.mkTerm(ext_30_0, {x});
37
38  Op ext_31_31 = tm.mkOp(Kind::BITVECTOR_EXTRACT, {31, 31});
39  Term x_31_31 = tm.mkTerm(ext_31_31, {x});
40
41  Op ext_0_0 = tm.mkOp(Kind::BITVECTOR_EXTRACT, {0, 0});
42  Term x_0_0 = tm.mkTerm(ext_0_0, {x});
43
44  Term eq = tm.mkTerm(Kind::EQUAL, {x_31_1, x_30_0});
45  cout << " Asserting: " << eq << endl;
46  slv.assertFormula(eq);
47
48  Term dis = tm.mkTerm(Kind::DISTINCT, {x_31_31, x_0_0});
49  cout << " Check sat assuming: " << dis << endl;
50  cout << " Expect UNSAT." << endl;
51  cout << " cvc5: " << slv.checkSatAssuming(dis) << endl;
52
53  return 0;
54}