Sum of all the bits in a Bit Vector of Z3

前端 未结 3 1235
长情又很酷
长情又很酷 2021-01-07 11:28

Given a bit vector in Z3, I am wondering how can I sum up each individual bit of this vector?

E.g.,

a = BitVecVal(3, 2)
sum_all_bit(a) =         


        
3条回答
  •  清歌不尽
    2021-01-07 11:54

    So you're computing the Hamming Weight of a bit vector. Based on a previous question I had, one of the developers had this answer. Based on that original answer, this is how I do it today:

    def HW(bvec):
        return Sum([ ZeroExt(int(ceil(log2(bvec.size()))), Extract(i,i,bvec)) for i in range(bvec.size())])
    

提交回复
热议问题