unsigned size_c = utils::getSize(c);
unsigned msb_x_pos = utils::getSize(x) - 1;
// (1 << (n - 1)))
- BitVector bv_msb_x = BitVector(size_c).setBit(msb_x_pos, true);
+ BitVector bv_msb_x(size_c);
+ bv_msb_x.setBit(msb_x_pos, true);
// (~0 << (n - 1))
BitVector bv_upper_bits =
(~BitVector(size_c)).leftShift(BitVector(size_c, msb_x_pos));
unsigned msb_x_pos = utils::getSize(x) - 1;
Node c_lo = utils::mkConst(bv_c.extract(msb_x_pos, 0));
// (1 << (n - 1)))
- BitVector bv_msb_x = BitVector(size_c).setBit(msb_x_pos, true);
+ BitVector bv_msb_x(size_c);
+ bv_msb_x.setBit(msb_x_pos, true);
// (~0 << (n - 1))
BitVector bv_upper_bits =
(~BitVector(size_c)).leftShift(BitVector(size_c, msb_x_pos));
return d_value.hash() + d_size;
}
-BitVector BitVector::setBit(uint32_t i, bool value) const
+BitVector& BitVector::setBit(uint32_t i, bool value)
{
CheckArgument(i < d_size, i);
- Integer res = d_value.setBit(i, value);
- return BitVector(d_size, res);
+ d_value.setBit(i, value);
+ return *this;
}
bool BitVector::isBitSet(uint32_t i) const
BitVector BitVector::mkMinSigned(unsigned size)
{
CheckArgument(size > 0, size);
- return BitVector(size).setBit(size - 1, true);
+ BitVector res(size);
+ res.setBit(size - 1, true);
+ return res;
}
BitVector BitVector::mkMaxSigned(unsigned size)
/**
* Set bit at index 'i' to given value.
+ * Returns a reference to this bit-vector to allow for chaining.
+ *
* value: True to set bit to 1, and false to set it to 0.
*
* Note: Least significant bit is at index 0.
*/
- BitVector setBit(uint32_t i, bool value) const;
+ BitVector& setBit(uint32_t i, bool value);
/** Return true if bit at index 'i' is 1, and false otherwise. */
bool isBitSet(uint32_t i) const;
bool sign)
{
BitVector bvsign = sign ? BitVector::mkOne(1) : BitVector::mkZero(1);
- BitVector bvexp =
- BitVector::mkOnes(size.packedExponentWidth()).setBit(0, false);
+ BitVector bvexp = BitVector::mkOnes(size.packedExponentWidth());
+ bvexp.setBit(0, false);
BitVector bvsig = BitVector::mkOnes(size.packedSignificandWidth());
return FloatingPoint(size, bvsign.concat(bvexp).concat(bvsig));
}
return !extractBitRange(1, i).isZero();
}
-Integer Integer::setBit(uint32_t i, bool value) const
+void Integer::setBit(uint32_t i, bool value)
{
cln::cl_I mask(1);
mask = mask << i;
- if (value) return Integer(cln::logior(d_value, mask));
- mask = cln::lognot(mask);
- return Integer(cln::logand(d_value, mask));
+ if (value)
+ {
+ d_value = cln::logior(d_value, mask);
+ }
+ else
+ {
+ mask = cln::lognot(mask);
+ d_value = cln::logand(d_value, mask);
+ }
}
Integer Integer::oneExtend(uint32_t size, uint32_t amount) const
/** Return true if bit at index 'i' is 1, and false otherwise. */
bool isBitSet(uint32_t i) const;
- /**
- * Returns the Integer obtained by setting the ith bit of the
- * current Integer to 1.
- */
- Integer setBit(uint32_t i, bool value) const;
+ /** Set the ith bit of the current Integer to 'value'. */
+ void setBit(uint32_t i, bool value);
/**
* Returns the integer with the binary representation of 'size' bits
return Integer(result);
}
-Integer Integer::setBit(uint32_t i, bool value) const
+void Integer::setBit(uint32_t i, bool value)
{
- mpz_class res = d_value;
if (value)
{
- mpz_setbit(res.get_mpz_t(), i);
+ mpz_setbit(d_value.get_mpz_t(), i);
}
else
{
- mpz_clrbit(res.get_mpz_t(), i);
+ mpz_clrbit(d_value.get_mpz_t(), i);
}
- return Integer(res);
}
bool Integer::isBitSet(uint32_t i) const
/** Return this*(2^pow). */
Integer multiplyByPow2(uint32_t pow) const;
- /**
- * Returns the Integer obtained by setting the ith bit of the
- * current Integer to 1.
- */
- Integer setBit(uint32_t i, bool value) const;
+ /** Set the ith bit of the current Integer to 'value'. */
+ void setBit(uint32_t i, bool value);
/** Return true if bit at index 'i' is 1, and false otherwise. */
bool isBitSet(uint32_t i) const;
void SetUp() override
{
d_zero = BitVector(4);
- d_one = d_zero.setBit(0, true);
+ d_one = BitVector::mkOne(4);
d_two = BitVector("0010", 2);
d_neg_one = BitVector(4, Integer(-1));
d_ones = BitVector::mkOnes(4);
TEST_F(TestUtilBlackBitVector, setBit_getBit)
{
- ASSERT_EQ(d_one.setBit(1, true).setBit(2, true).setBit(3, true), d_ones);
- ASSERT_EQ(d_ones.setBit(0, false).setBit(1, false).setBit(2, false).setBit(
- 3, false),
- d_zero);
- ASSERT_EQ(d_ones.setBit(0, false).setBit(0, true), d_ones);
- ASSERT_EQ(d_ones.setBit(0, false), ~BitVector::mkOne(d_one.getSize()));
+ BitVector ones(d_one);
+ ASSERT_EQ(ones.setBit(1, true).setBit(2, true).setBit(3, true), d_ones);
+
+ BitVector zero(d_ones);
+ ASSERT_EQ(
+ zero.setBit(0, false).setBit(1, false).setBit(2, false).setBit(3, false),
+ d_zero);
+
+ ones = d_ones;
+ ASSERT_EQ(ones.setBit(0, false).setBit(0, true), d_ones);
+
+ BitVector not_one(d_ones);
+ ASSERT_EQ(not_one.setBit(0, false), ~BitVector::mkOne(d_one.getSize()));
ASSERT_TRUE(d_ones.isBitSet(3));
ASSERT_FALSE(d_two.isBitSet(3));