Skip to content

Commit e2f2c22

Browse files
author
Daniel Kroening
committed
added means to get a bit from bit-vector representation
1 parent ec8b34d commit e2f2c22

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/solvers/flattening/boolbv_constant.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author: Daniel Kroening, [email protected]
66
77
\*******************************************************************/
88

9+
#include <util/arith_tools.h>
910

1011
#include "boolbv.h"
1112

@@ -78,9 +79,9 @@ bvt boolbvt::convert_constant(const constant_exprt &expr)
7879
expr_type.id()==ID_c_bit_field ||
7980
expr_type.id()==ID_incomplete_c_enum)
8081
{
81-
const std::string &binary=id2string(expr.get_value());
82+
const auto &value = expr.get_value();
8283

83-
if(binary.size()!=width)
84+
if(value.size() != width)
8485
{
8586
error().source_location=expr.find_source_location();
8687
error() << "wrong value length in constant: "
@@ -90,7 +91,7 @@ bvt boolbvt::convert_constant(const constant_exprt &expr)
9091

9192
for(std::size_t i=0; i<width; i++)
9293
{
93-
bool bit=(binary[binary.size()-i-1]=='1');
94+
const bool bit = get_bitvector_bit(value, i);
9495
bv[i]=const_literal(bit);
9596
}
9697

src/util/arith_tools.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,14 @@ void mp_max(mp_integer &a, const mp_integer &b)
262262
if(b>a)
263263
a=b;
264264
}
265+
266+
/// Get a bit with given index from bit-vector representation.
267+
/// \param src: the bitvector representation
268+
/// \param bit_index: index (0 is the least significant)
269+
bool get_bitvector_bit(const irep_idt &src, std::size_t bit_index)
270+
{
271+
// The representation is binary, using '0'/'1',
272+
// most significant bit first.
273+
PRECONDITION(bit_index < src.size());
274+
return src[src.size() - 1 - bit_index] == '1';
275+
}

src/util/arith_tools.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,9 @@ mp_integer power(const mp_integer &base, const mp_integer &exponent);
164164
void mp_min(mp_integer &a, const mp_integer &b);
165165
void mp_max(mp_integer &a, const mp_integer &b);
166166

167+
/// get bit with given index from bit vector
168+
/// \param src: the bitvector representation
169+
/// \param bit_index: the index (0 is the least significant)
170+
bool get_bitvector_bit(const irep_idt &src, std::size_t bit_index);
171+
167172
#endif // CPROVER_UTIL_ARITH_TOOLS_H

0 commit comments

Comments
 (0)