/// \returns true if this APInt is non-positive.
bool isNonPositive() const { return !isStrictlyPositive(); }
+ /// Determine if this APInt Value only has the specified bit set.
+ ///
+ /// \returns true if this APInt only has the specified bit set.
+ bool isOneBitSet(unsigned BitNo) const {
+ return (*this)[BitNo] && countPopulation() == 1;
+ }
+
/// Determine if all bits are set. This is true for zero-width values.
bool isAllOnes() const {
if (BitWidth == 0)
}
}
+TEST(APIntTest, isOneBitSet) {
+ EXPECT_FALSE(APInt(5, 0x00).isOneBitSet(0));
+ EXPECT_FALSE(APInt(5, 0x02).isOneBitSet(0));
+ EXPECT_FALSE(APInt(5, 0x03).isOneBitSet(0));
+ EXPECT_TRUE(APInt(5, 0x02).isOneBitSet(1));
+ EXPECT_TRUE(APInt(32, (unsigned)(0xffu << 31)).isOneBitSet(31));
+
+ EXPECT_TRUE(APInt::getOneBitSet(255, 13).isOneBitSet(13));
+}
+
TEST(APIntTest, isPowerOf2) {
EXPECT_FALSE(APInt(5, 0x00).isPowerOf2());
EXPECT_FALSE(APInt(32, 0x11).isPowerOf2());